none
Can Entity Framework be used successfully with a database having tables with nothing but primary keys? RRS feed

  • Question

  • I am working with my employers database and realized the only constraints in the database itself are primary key constraints. There are obviously logical foreign keys relationships between tables but no foreign key constraints in place. 

    This database has migrated through the years to SQL server 2008 but was done many years ago. The relational integrity is maintained by a custom data access layer and hand rolled ORM in an old VB6 program. Unfortunately newer application have to be designed around this same database. 

    Is there anyway, assuming we understand all of the intended foreign key relationships, that we could use Entity Framework, (maybe code first) against the database? That way we could gain the benefits of the framework and the relationships between entities enabling more modern data access using Linq to Entity Framework.

    Please key in mind that re-architecting the database at this time is not an option, but might be sometime down the road. 

    Any ones opinions and experience would be greatly welcomed.

    Thanx in advance...

    Thursday, August 28, 2014 12:37 AM

Answers

  • Yes, you can model the relationships in code, and avoid using Migrations.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by preCompile Wednesday, September 3, 2014 3:08 PM
    Thursday, August 28, 2014 7:14 AM

All replies

  • Yes, you can model the relationships in code, and avoid using Migrations.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by preCompile Wednesday, September 3, 2014 3:08 PM
    Thursday, August 28, 2014 7:14 AM
  • When you model the relationships in code, are you referring to uses the code first part of EF?

    I am not familiar with Migrations, what benefits or detriment in my case would they cause? Sorry for responding so late. I thought I had email notifications turned on. 

    Edit: Please disregard the question on migrations I just researched and understand the concept now.

    There is one thing I am not understanding completely with using code first (EF5)

    I plan of using the declarative attributes on all pocos just for clarity as opposed to convention. Will foreign key attributes need to be defined on the entities to allow for the relationships between the entities? If so will the foreign key constraints at the model level be enforced even though none really exist in the database? 

    The reason I ask the above is that I am looking not towards a method to constraint the data going into the database by using code first, but to merely be able to access the data and and the logical relationships between the table data that already exists.

    • Edited by preCompile Monday, September 1, 2014 4:25 AM Did some research that should have been done before asking the question
    Monday, September 1, 2014 2:48 AM
  • You cannot use attributes for modeling relationships, you must use the fluent API, but that will alow you to model relationships and enforce relations


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Monday, September 1, 2014 6:48 AM
  • I have read a couple of articles about the fluent api. In the example http://msdn.microsoft.com/en-us/data/hh134698.aspx specifically they still show defining a foreign key, with HasForeignKey.

    Is it possible to use HasRequired and WithMany only for the relationship or do I need to define a foreign key for the model even though the database does not have foreign key constraint defined in it.

    Please remember the database does not have foreign key constraints defined only primary keys and nothing else.

    Thanks

    Wednesday, September 3, 2014 2:25 AM
  • Yes, you need to define foreign keys for the model, even though they are not enforced by the database schema.

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, September 3, 2014 5:41 AM