EF Beta 3 LINQ RRS feed

  • Question

  • Dear all,


    I have used the EF wizard in Beta 3 to model on existing SQL Express Northwind database.  What puzzles me how does EF maps back to the database?.  Can someone point out where this is done?  Thanks.




    Wednesday, January 9, 2008 7:46 AM


All replies

  • Ooh - one of my favorite topics!


    The designer is only showing you what is most relevant to building the model. What it is displaying is based on the contents of the EDMX file which is an XML file. You can see the raw xml by right clicking on the EDMX file in the solution explorer, choosing Open With, then XML Editor. In there you'll first see a section of XML that tells the designer how to display the entities, but after that there are three chunks of XML, the Conceptual Layer (which you are visualizing in the designer), the STORE Layer (which is a representation of the database schema) and then the Mapping Layer which describes how we get from the entities, properties and association of the Conceptual Layer to the entities (tables), properties (columns) and associations (relationships) laid out in the store layer.


    The EF APIs read the model to figure out how to rebuild your queries into queries that will make sense to the database, then execute them. There much more going on here with stored procedures, etc, but this is the basic concept.


    I wrote an Intro to Entity Framework article for CoDe Magazine that spends more time talking about how the model works. It's here ... [http://www.code-magazine.com/Article.aspx?quickid=0711051]


    plus there's great info in the EF documentation as well as this great blog post from the ADO.NET Team blog [http://blogs.msdn.com/adonet/archive/2007/02/26/mapping-101-part-1.aspx]




    Wednesday, January 9, 2008 2:58 PM
  • Hi,


    Thanks for the prompt reply.  I am looking at how to extend the way data is inserted into database.  In LINQ to SQL, I see that you can extend it by having a store procedure to do insert instead of native SQL insert statement.  I am still confused with the way EF works.  Let say if I model the EDM with the source database from MS SQL and in theory, if I have the exact same datastructure in Oracle (provided that the extended data provider is available) then I need just to swap change the data source in the config file.  Then the application should not break.


    I have data tables that has its primary key being generated via stored procedure and it is not possible for me to use the built-in identity column.  How is it possible to extend the EDM/EF to do the custom generation of the primary key in an insert scenario.  Thanks.




    Wednesday, January 9, 2008 5:08 PM

    Entity Framework offers an easy way to deal with server-generated values. You’ll need the following two things:

    ·         An IDENTITY column or an ON INSERT trigger that generates sequence values.

    ·         In SSDL (or the SSDL portion of the EDMX file) mark that property with StoreGeneratedPattern="Identity", e.g. http://msdn2.microsoft.com/en-us/library/bb399579.aspx.


    Zlatko Michailov

    Program Manager, Entity Services

    Microsoft Corp.


    Wednesday, January 9, 2008 5:51 PM
  • Kueh,


     To better understand your scenario I would like to know why you are generating the sequence number in a stored procedure rather than allowing SQL Server to do this for you.


     To answer your question directly, yes, you can bind the insert behavior of an entity type to a stored procedure and bind the stored procedures return column to the entity type's key. But before delving into that more deeply, I'd like to understand why there is a stored procedure there in the first place.





    Wednesday, January 9, 2008 6:41 PM
  • Note that it is not guaranteed for it to be possible to simple point an EDMX file generated for SQL Server and have it magically work with an Oracle database: The SSDL sections for the two database may require some changes.


    That said, for a demo of the stored procedure mapping capabilities of the designer, please see this:


    This will show exactly how to map to a stored procedure as well as how to get back the generated id and put it into the entity type's key field.


    You may also want to look at this for some more general mapping information:


    Wednesday, January 9, 2008 7:09 PM
  • Noam,


    In my case the primary key a concatination of two integer number of fixed length each, the first part is region and the second part is a running sequence number.  The problem is that I cannot change it as existing apps is using the database.  Thanks.





    Thursday, January 10, 2008 3:31 AM