entity-framework-4.1

Why does Entity Framework return null List<> instead of empty ones?

大城市里の小女人 提交于 2019-11-30 16:54:38
I'm pretty new in the ASP .NET MVC world. Maybe, that's the reason I can't explain to myself the cause of what is, for me, an annoying problem. I have one class with One-To-Many relashionship. class MyClass{ public List<OtherClass> otherClasses {get;set;} } When I'm persisting one instance of this class, I fill it's relationship with an empty List<> MyClass myClass = new MyClass(){ otherClasses = new List<OtherClass>() } context.myClass.Add(myClass); The problem is that, when I try to retrieve that instance, and for any reason, I try to access that list, system gives me a Null Reference

How can I use TPT inheritance models when primary keys have different names?

久未见 提交于 2019-11-30 16:25:24
Using Entity Framework 4.1 against a legacy database, I'm unable to generate a working set of TPT inheritance models that aren't pluralized and use different names for a common primary key. I am using database tables Organization, Account, and Company as illustrated below: Organization OrganizationID (int PK) OrgName (varchar) Company CompanyID (int PK) CompanyNo (varchar) Account AccountID (int PK) AccountNo (varchar) Account.AccountID and Company.CompanyID have a FK constraint that values in those columns must also be contained in Organization.OrganizationID so neither can exist without a

EFCode First Property Null problem

放肆的年华 提交于 2019-11-30 16:14:23
问题 I use EFCode First in asp.net mvc 3 model. (Entity Framework 4.0 and EFCode First 0.8) The model is defined like below: public class User { [Key] public int Id { get; set; } public string Name { get; set; } public int WorkedYears { get; set; } } when use db.Users.Find(1) , will throw this error: The 'WorkedYears' property on 'User' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'. Note: user.Id=1 exists in database, and WorkedYears of the

EFCode First Property Null problem

孤街醉人 提交于 2019-11-30 16:04:34
I use EFCode First in asp.net mvc 3 model. (Entity Framework 4.0 and EFCode First 0.8) The model is defined like below: public class User { [Key] public int Id { get; set; } public string Name { get; set; } public int WorkedYears { get; set; } } when use db.Users.Find(1) , will throw this error: The 'WorkedYears' property on 'User' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'. Note: user.Id=1 exists in database, and WorkedYears of the record is NULL. if I set the WorkedYears = 0 in the database, the error will disappear, and also if I

EF4 Code First: how to update specific fields only

本小妞迷上赌 提交于 2019-11-30 15:21:29
问题 How do I update only certain fields on an entity? I have a User entity like so: public class User { public string UserId { get; set; } public string PasswordHash { get; set; } public bool IsDisabled { get; set; } public DateTime AccessExpiryDate { get; set; } public bool MustChangePassword { get; set; } public DateTime DateCreated { get; set; } public DateTime LastActivity { get; set; } } So if for example, I want to update the user entity, but do not want to change the user password, how do

EF CodeFirst create non-clustered primary key index

佐手、 提交于 2019-11-30 15:00:51
问题 I'm using EF 4.1 CodeFirst to create my DB. It seems that EF is creating all primary keys with clustered index, which is not optimal for us in one case(possibly more cases). Is there a way to tell EF to generate this table with primary key as a non-clustered index ? Of course we could do it manually using custom script after database is already created, but we have a lot of foreign keys pointing to this table, it would be quite a non-optimal solution if there is a better, more straight

The Include path expression must refer to a navigation property defined on the type

ぐ巨炮叔叔 提交于 2019-11-30 14:35:45
问题 I have the following Repository method:- public AccountDefinition GetCustomer2(int id) { var c = entities.AccountDefinitions .Where(p=>p.ORG_ID==id) .Include(a => a.SDOrganization) .Include(a2 => a2.SiteDefinitions) .Include(a3 => a3.SDOrganization.AaaPostalAddresses) .Include(a4 => a4.SiteDefinitions.SelectMany (a5 => a5.DepartmentDefinitions.SelectMany (a6 => a6.SDUsers.Select (a7 => a7.AaaUser)))) .SingleOrDefault(); return c; } The the following action method which calls the above method:

Updating my EF model to use 4.1 when I built it in 4.0

爱⌒轻易说出口 提交于 2019-11-30 13:54:59
I built my EF Model in EF 4.0, and then installed the 4.1 upgrade that includes the new DBContext interface. How do I update my model so that it uses the 4.1 features going forward? Thank You Ladislav Mrnka You can use DbContext with your EDMX model. After installing EFv4.1 you should have new T4 template available: DbContext generator. This will take your EDMX and create context derived from DbContext and all POCO entities for you. Here you have walkthrough. But if you want to switch to DbContext just because of DbContext.Entry.State you don't have to. EFv4 has a similar mechanism: context

Migration using model first approach in entity framework

岁酱吖の 提交于 2019-11-30 13:49:48
问题 I have setup a system where I have taken the model first approach as it made more logical sense for me. Now when even I have some changes in the model currently what I do is - Use the Generate database from model feature of entity framework. I create a dummy database and apply those scripts. which deletes all my data and tables first and then updates the database with the latest sql file which is generated by entity framework. Now I use the Visual Studio's schema compare feature and generate

EF CodeFirst create non-clustered primary key index

别来无恙 提交于 2019-11-30 13:37:14
I'm using EF 4.1 CodeFirst to create my DB. It seems that EF is creating all primary keys with clustered index, which is not optimal for us in one case(possibly more cases). Is there a way to tell EF to generate this table with primary key as a non-clustered index ? Of course we could do it manually using custom script after database is already created, but we have a lot of foreign keys pointing to this table, it would be quite a non-optimal solution if there is a better, more straight forward way to do the same already during DB creation. Any help appreciated Ladislav Mrnka No there is no way