entity-framework-4.1

How to Get Full Object with Code First Entity Framework 4.1

心已入冬 提交于 2019-12-10 16:55:43
问题 I am trying to return as JSON the fully deep object (with all of the foreign key relationships filled in) but I am getting nulls for all the referenced objects. Here is the call to get the object: public ActionResult GetAll() { return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet); } And here is the Order object itself: public class Order { public int Id { get; set; } public Patient Patient { get; set; } public CertificationPeriod CertificationPeriod { get; set; } public Agency Agency

Entity Framework Code First One-To-Many Relation Mapping with link table

徘徊边缘 提交于 2019-12-10 16:50:05
问题 I have a question about One-To-Many relationship between two tables when there is the link (join) table between. Example tables: ChildTable: ID int NOT NULL PK Relation int NOT NULL ParentTable: ID int NOT NULL PK Name nvarchar(50) NOT NULL ParentChildren: ParentTable_ID int NOT NULL PFK ChildTable_ID int NOT NULL PFK Entities: public class ChildTable { public ChildTable() { this.ParentTables = new List<ParentTable>(); } public int ID { get; set; } public int Relation { get; set; } public

Best solution for EF 4.1 + MVC + JSON circular reference exception?

£可爱£侵袭症+ 提交于 2019-12-10 15:57:59
问题 I'm using EF 4.1 Database First approach, with T4 template generating my POCO classes in separate assembly. I have repositories for fetching data, and service layer which is used for communication with UI. I was trying to make cascading dropdowns. I'm new in MVC and EF 4.1, so I searched stackoverflow for possible solutions. This is sample viewmodel class: public class MyViewModel { public int CustomerId { get; set; } public string CustomerName { get; set; } public IEnumerable<Phone> Phones {

How do I get EF to persist empty strings as NULL?

故事扮演 提交于 2019-12-10 15:48:28
问题 In my domain, there's no important distinction between NULL and an empty string. How do I get EF to ignore the difference between the two and always persist an empty string as NULL? 回答1: Empty string is not default value for string property so it means your code is setting empty strings somewhere. In such case it is your responsibility to handle it. If you are using code first with POCOs you can use custom setter: private string _myProperty; public string MyProperty { get { return _myProperty

Ninject scope problems with TopShelf, Ninject and EF code first

岁酱吖の 提交于 2019-12-10 14:44:46
问题 I am currently using TopShelf with Ninject to create a Windows Service. I have the following code to setup the Windows Service using TopShelf: static void Main(string[] args) { using (IKernel kernel = new StandardKernel(new NinjectDependencyResolver())) { Settings settings = kernel.Get<Settings>(); var host = HostFactory.New(x => { x.Service<BotService>(s => { s.ConstructUsing(name => new BotService(settings.Service.TimeInterval)); s.WhenStarted(ms => ms.Start()); s.WhenStopped(ms => ms.Stop(

How to prevent EF from validating properties that are not mapped during DBContext.SaveChanges()

早过忘川 提交于 2019-12-10 13:26:51
问题 I have a user model with two [NotMapped] string properties Password and ConfirmPassword. These are unmapped because I save password as byte array (after salting) so there are two additional properties (mapped) InternalPassword and Salt in user model. The problem is when I use the user model to change password, entity framework throws DBEntityValidation error stating "The Password property is required." What I understand here is that EF is trying to validate my model before saving and since

What are best practices for managing DataContext?

﹥>﹥吖頭↗ 提交于 2019-12-10 12:15:36
问题 In an effort to make my entities persistent ignorant and make my repositories testable, I've implemented a repository pattern like so: public interface IJobRepository : IRepository<Job> { Job GetJobById(int jobId); //Special case where I'm eager loading other entities void SaveJob(Job job, Job originalJob); } public class JobRepository : IJobRepository { private readonly IContext _context; public JobRepository() { _context = new CustomObjectContext(); } public JobRepository(UnitOfWork

Is it possible to create User-Defined Data Types when using EF 4.1 Code First?

谁说胖子不能爱 提交于 2019-12-10 11:41:32
问题 When using EF 4.1 Code First, is it possible to create User-Defined Data Types for your schema? 回答1: Simple answer is no. Longer answer: Current EF implementation leads to multiple issues when trying to use user defined types: The type must be defined prior to its usage in table's DDL definition. Because of that the type cannot be defined in Seed method of database initializer (as often used for other database constructs like triggers or indexes). To make this work you must create whole new

Entity Framework 4.1 Code First Freeze

拥有回忆 提交于 2019-12-10 11:34:53
问题 I'm having trouble getting EF 4.1 working on my computer. It seems to be some problem with my database settings. I was trying out this simple walkthrough: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx But when it reaches db.Categories.Add(food); it just freeze. I have a normal SQL Server 2008 R2 installed, not SQL Express. There also seems to be some problems with creating .mdf files instead of a direct connection to the localhost SQL server. I've also

Entity Framework 4.1 DbContext generator problems

。_饼干妹妹 提交于 2019-12-10 11:32:42
问题 I am new to Entity Framework 4.1 and I really wanted to transition to POCO classes for my model. I found that this was very easy using the "DbContext Generator" item provided when you install EF 4.1. It did exactly what I wanted it to do and generated the DbContext object and all the POCOs for my existing EDMX model. I ran the app and tested that it was still working. It was. Happy with this I deleted the EDMX file and the T4 templates and began reorganizing my new POCOs. However, after