0

I have worked on a project with Code-first Approach and know I need to convert it to Database-first Approach, because I have some logic I have to executed it through Stored-Procedure.

So, I decided to Convert my Project from code-first to database-first

Anyhow, I have moved the identity tables to the new database.

I have got this Error,

The property 'Claims' on type 'AspNetUser' is not a navigation property. The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method.

and in the OnModelCreating I have added these lines

    modelBuilder.Entity<AspNetUser>().ToTable("AspNetUsers");
    modelBuilder.Entity<AspNetUserRole>().ToTable("AspNetUserRoles");
    modelBuilder.Entity<AspNetRole>().ToTable("AspNetRoles");
    modelBuilder.Entity<AspNetUserClaim>().ToTable("AspNetUserClaims");
    modelBuilder.Entity<AspNetUserLogin>().ToTable("AspNetUserLogins");

    modelBuilder.Entity<AspNetUser>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    modelBuilder.Entity<AspNetRole>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    modelBuilder.Entity<AspNetUserClaim>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

any Solution for that ?

Update (1)

When I change the connection string it gives me these errors

One or more validation errors were detected during model generation: AwesomeMvcDemo.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. AwesomeMvcDemo.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

Rajput
  • 2,597
  • 16
  • 29
Loai
  • 732
  • 16
  • 32
  • First, OnModelCreating is for Code First not EDMX. Second, you can use stored procedures in Code First. [Switching back to database first](http://stackoverflow.com/questions/18879513/entity-framework-5-switch-from-code-first-to-database-first) – Steve Greene Dec 28 '16 at 17:45
  • @SteveGreene I have use OnModelCreating to relate the Entities with Associate table in database, if i create a Stored Procedure in the database that will removed when i try to re Initialize the dataabse, right ? – Loai Dec 28 '16 at 17:50
  • @SteveGreene i have not found any thing related to my question in your posted URL – Loai Dec 28 '16 at 17:50
  • 1
    There are various [techniques](http://www.mikesdotnetting.com/article/299/entity-framework-code-first-and-stored-procedures) for seeding the stored procedures so they don't get wiped on database creation. – Steve Greene Dec 28 '16 at 17:57
  • thanks, for that, but for now, i need to switch my project to Database-first cause it's an urgent. – Loai Dec 28 '16 at 18:02
  • Ok, my first link talks about that. [Here is another](http://stackoverflow.com/questions/21732865/how-to-convert-my-ef-code-first-to-database-first). Basically you will need to generate the EDMX and classes. If you have annotations you want to preserve, you will need to create partial classes that don't get wiped as the models change. – Steve Greene Dec 28 '16 at 18:07
  • @SteveGreene Thank you so much, i have look at that and do everything related to that and i have add and configure partial classes as well, but after that i still got the error which is in My Question. – Loai Dec 28 '16 at 18:10
  • The OnModelCreating is not relevant in Database First, that is for code first. Is your context inheriting from IdentityDbContext? What's the connect string look like? – Steve Greene Dec 28 '16 at 18:24
  • @SteveGreene ok, i have remove everything in the `OnModelCreating` – Loai Dec 28 '16 at 18:25
  • Connection Strings – Loai Dec 28 '16 at 18:26
  • @SteveGreene i have Update the Post please refer to it. – Loai Dec 28 '16 at 18:30
  • I suspect that what is happening is that the generated classes added some navigation properties that are already coming from the base class (IdentityUser). You may need to comment out some of those properties. – Steve Greene Dec 28 '16 at 18:33
  • Compare the code first user class with the one generated by database first. – Steve Greene Dec 28 '16 at 18:35
  • I'm using Identity 2.0, so it has too many properties more that Identity 1.0, anyhow, i have add some Properties in the database that are not in the Identity 2.0. after that i get this error `Unable to load the specified metadata resource` – Loai Dec 28 '16 at 18:40
  • You can invoke stored procedures when using Code First. – Pawel Dec 28 '16 at 19:02
  • You update your model after making changes? http://stackoverflow.com/questions/14648212/unable-to-load-the-specified-metadata-resource – Steve Greene Dec 28 '16 at 20:02
  • @Pawel for now, i have add a custom stored procedure in database, and i use EDMX to get and deal with that Stored Procedure, is that a good way to do that ? for me if some one can help me to convert my project to Database-first it will be better. i tried a lot but i stuck to the IDENTITY Issues. – Loai Dec 29 '16 at 09:23
  • @SteveGreene i have not change anything. – Loai Dec 29 '16 at 09:24

0 Answers0