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

You should have your entity create those lists in the constructor. EF doesn’t create dependent collections, and expects the entity to do so. So, your case, you would make your entity like this: class MyClass{ public List<OtherClass> _otherClasses {get;set;} public MyClass() { _otherClasses = new List<OtherClass>(); } }

Repository Pattern with Entity Framework 4.1 and Parent/Child Relationships

The primary reason why I want to use this pattern is to avoid calling EF 4.1 specific data access operations from the domain. I’d rather call generic CRUD operations from a IRepository interface. This will make testing easier No it will not make your testing easier. You exposed IQueryable so your repository is not unit … Read more

Entity Framework Validation confusion – maximum string length of ‘128’

Default length of string field in code first is 128. If you are using EF validation it will throw exception. You can extend the size by using: [StringLength(Int32.MaxValue)] public string Body { get; set; } This post became somehow popular so I’m adding second approach which also works: [MaxLength] public string Body { get; set; … Read more

Entity Framework code first. Find primary key

You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>(); IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k => k.Name); Once you have key names you can use reflection to access their values. As you can see the approach reverts back to … Read more

Reload an entity and all Navigation Property Association- DbSet Entity Framework

If you don’t use lazy loading, you have the load the new Address explicitly (as you had to load it explicitly (with Include, for example), when you loaded the Person initially): context.Entry(myPerson).Reload(); // If the person refers to another Address in the DB // myPerson.Address will be null now if (myPerson.Address == null) context.Entry(myPerson).Reference(p => … Read more

Entity Framework Code First – Defining Relationships/Keys

This is exception caused by SQL server when you have multiple paths of cascade deletes. If you delete your PaymentTerm it will trigger cascade delete on all three relations. This will blow up when creating either SalesOrder or Invoice. EF creates by default all one-to-many relations with ON DELETE CASCADE you can remap your specific … Read more

Entity Framework 4.1 InverseProperty Attribute

I add an example for the InversePropertyAttribute. It cannot only be used for relationships in self referencing entities (as in the example linked in Ladislav’s answer) but also in the “normal” case of relationships between different entities: public class Book { public int ID {get; set;} public string Title {get; set;} [InverseProperty(“Books”)] public Author Author … Read more

Conflicting changes to the role x of the relationship y have been detected

The problem is this one: MyEntity has an ID of 0 since it’s a new MyEntity. The Group is also new and contain a reference to MyEntity. So, MyEntity contains a list of Group which contain a reference back to MyEntity. The problem is that MyEntity.Group.MyEntity seems to be “new and not the same” as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)