Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad an .edmx into the DbModelBuilder
    primarykey
    data
    text
    <p>I'm planning on using the Entity Framework 4.1 in my next project, but I'm having trouble finding a good way to go about it. In short, I want to build a multi-tiered application in which the entities will be travelling through web services, and to keep it all as clean as possible I want to use POCO's rather than self tracking entities. Also, there already exists a SQL 2008 database that will be used to base the entities on.</p> <p>From what I've read so far (from Julie Lerman's article on <a href="http://msdn.microsoft.com/nl-nl/magazine/hh148150%28en-us%29.aspx" rel="nofollow">http://msdn.microsoft.com/nl-nl/magazine/hh148150%28en-us%29.aspx</a>, amongst others), it seems that:</p> <ul> <li>If you use the Database First approach, you get a beautiful .edmx to edit your model in, but you'll always end up with persistence-aware objects rather than POCO's, which is not useful in my situation.</li> <li>If you use the Code First approach, the "ADO.NET DbContext generator" only partially helps you: it does generate entities from the .edmx, but it doesn't generate the code required to get the foreign keys and cardinality correct. This means that the code will not work out-of-the-box (-<strong>edit</strong>, not true, see my post below-), you either have to <ul> <li>a) use Data Annotations on your POCO's, which is ugly imo because it pollutes the POCO's with database information and also creates a dependency on the EntityFramework assembly.</li> <li>b) use the DbModelBuilder passed to DbContext.OnModelCreating to set the correct foreign key, mapping etc. information (i.e. the 'fluent' API). And even though the API may be 'fluent', it's still pretty hard (and probably unmaintainable) to set all this information correctly so that it matches the existing database (see <a href="http://sessionfactory.blogspot.com/2011/04/conventions-in-entity-framework-41.html" rel="nofollow">http://sessionfactory.blogspot.com/2011/04/conventions-in-entity-framework-41.html</a> for some examples of this).</li> </ul></li> </ul> <p>I realize that the reason why the "DbModelBuilder-way" requires so much effort is because it was designed to be used the other way around: you're supposed to generate the database from the Entity definitions, not try to tweak all Entities so that they (hopefully) match an already existing database. However, it seems to me that the "DbModelBuilder-way" will, in the end, produce the best result: pure POCO's with no database metadata in them.</p> <p>Now, having said all this, my question is:<br> <strong>Does anyone know of a way to load an .edmx into the DbModelBuilder</strong>, so that the foreign key, column mapping and other information doens't need to be specified by hand through the fluent API?</p> <p>I think that this would be the best of both worlds, because you can visually edit the mapping like you would in the Database First scenario, and still get clean POCO's because all required metadata is stored in the DbModelBuilder.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload