c# - Entity Framework Many to Many Code FIrst -
c# - Entity Framework Many to Many Code FIrst -
i have next (simplified) classes:
using system; using system.collections.generic; using system.data.entity; using system.data.entity.modelconfiguration.conventions; using system.linq; using system.text; using system.threading.tasks; namespace eftest { public class textcontext : dbcontext { public dbset<people> people { get; set; } public dbset<file> files { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<manytomanycascadedeleteconvention>(); } } public class people { public people() { files = new list<file>(); } public int peopleid { get; set; } public string nombre { get; set; } public list<file> files { get; set; } } public class file { public file() { friends = new list<people>(); foes = new list<people>(); } public int16 fileid { get; set; } public list<people> friends { get; set; } public list<people> foes { get; set; } } }
when db created expect have joint table people , files. none created, table people , table files.
the next image of entity info model
i expected model show many many relationship each people may have many files , each file may have many people
i'm quite new ef, know need utilize fluent api configure relation far code have tried has failed.
thanks help
try setting related entities virtual
keyword ef can override , implement navigation properties itself. also, tend utilize interfaces properties instead on concrete classes. call...
public class people { public people() { files = new list<file>(); } public int peopleid { get; set; } public string nombre { get; set; } public virtual icollection<file> files { get; set; } } public class file { public file() { friends = new list<people>(); foes = new list<people>(); } public int16 fileid { get; set; } public virtual icollection<people> friends { get; set; } public virtual icollection<people> foes { get; set; } }
c# entity-framework many-to-many
Comments
Post a Comment