locked
Database first to code first problem (EF Power Tools) RRS feed

  • Question

  • Hi

    Backround story: We are doing some updating work to our project. We have just moved from Mvc 3 & .Net 4 to Mvc 5 & .Net 4.5 and since our server dosen't have .net 4.5 support yet all changes are just minimal tested locally. Next step was move to data base first to code first (and from manual handled sql scripts to ef migration). I have idea where problem most propably is (more about that later) but since I am new developer with this project and I am more professional on desktop side than asp.net / database I feel myself little uncomfortable since I am just "blindly" following quides without _deep_ understanding what there is happening (more about this later too).

    Our model structure was like generated model partial classes under Model folder and our own business functionality partial classes under Model/Extension. I did code first migration with Entity Framework Power tools beta 4 and following this guide: http://devgush.com/2014/02/24/migrating-a-project-from-database-first-to-code-first/

    Some exceptions: I deleted first old models & tt & edmx but old context I removed after enabling migration (I spesified to use new dbcontext for that operation). After that I replaced old dbcontext name with new one on entire project.

    Aaand the _big_exception_: Quide says that you should comment DbMigration.Up() method but I commented (accidently) Up and Down (and definition of Down).

    Anyway result was that I got all models that lived in database, all old models and lot of aspnet_[Foo] named models too. After trying to run program I get lot of "invalid column name..." errors. Most of these are result from fact that our partial business logic classes have lot of properties that are not really db related and adding [NotMapped] to them fixed those.

    After adding some [NotMapped] I got our application partially work and example login worked fine (yeii not totally broken!). Buut one of our very commonly used database object "Attribute" related model usage throw same error ("unknown column name"). Column was named "Attribute_AttributeID1" or "Attribute1_AttributeID" (btw Attribute has "AttributeID" named key column). I searched whole solution and only references to that name was in DbMigration Up() and Down() (and both were commented like I said earlier).

    Questions:
    I assume that "reverse engineer to code first" has generated some changes also to database and those changes are not applied since comment fault.
    A: Is this assumption right (or is that some kind of generation fault)?
    B: Why "reverse engineering" cause need to change orginal database?

    I thinked that mapping stuff was related to linkin code and Db but because there is nothing related to "Attribute_AttributeID1" where this word actually lives?

    Not very important: Have you any idea how to mark whole partial class to [NotMapped] or somehow else handle separated model structure / logic?

    Thanks for your time =)
    Thursday, June 18, 2015 11:43 AM

Answers

  • Hi

    Thanks for your response. I finally found problem from db itself. Attribute table had "Attribute_Attribute" named foreign key which was linked to its own primary key column.

    Monday, June 22, 2015 3:06 PM

All replies

  • Hello,

    >>I assume that "reverse engineer to code first" has generated some changes also to database and those changes are not applied since comment fault.

    As far as I know, this tool does not change the database schema as column name, table name and constraints, I also downloaded the tool and made a test according to the link you provided, however, it worked fine, I do not add the NotMapped attribute.

    >>I thinked that mapping stuff was related to linkin code and Db but because there is nothing related to "Attribute_AttributeID1" where this word actually lives?

    For your description scenario, it is not clear about it, please share a small demo or some code if possible.

    >>Not very important: Have you any idea how to mark whole partial class to [NotMapped] or somehow else handle separated model structure / logic?

    In the override OnModelCreating method, we could exclude a type from the model as:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
    
                modelBuilder.Ignore<LabRolePermission>();
    
            }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 19, 2015 6:00 AM
  • Hi

    Thanks for your response. I finally found problem from db itself. Attribute table had "Attribute_Attribute" named foreign key which was linked to its own primary key column.

    Monday, June 22, 2015 3:06 PM