ApplicationUser relacja one to one

ApplicationUser relacja one to one
UL
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 37
0

Po updacie bazy nie porafie zmapować tabeli "Customers" do ApplicationUsers w relacji one to one. Nie rozumiem, jak może nie pokrywać się klucz. Gdzie popełniam bląd?

Source.Database.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Source.Database.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
ApplicationUser_Customers_Target: : Multiplicity is not valid in Role 'ApplicationUser_Customers_Target' in relationship 'ApplicationUser_Customers'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '
'.
ApplicationUser_Customers_Source_ApplicationUser_Customers_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ApplicationUserId' on entity 'Customers' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'ApplicationUser_Customers'.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined

Kopiuj

public class ApplicationUser : IdentityUser
    {
        public virtual Customers Customers { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ApplicationUser>()
                .HasOptional(a => a.Customers)
                .WithRequired(ab => ab.ApplicationUser);
        }

public class Customers
    {
        [ForeignKey("ApplicationUser")]
        public int ApplicationUserId { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

SZ
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1591
0

A AplicationUser Id z tego co pamietam jest typu string. Czyli klucz obcy tez taki powinien byc

UL
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 37
0

@szydlak: Masz racje. Zmieniłem, ale to nie rozwiązuje problemu.

Gdy dodaje kod

Kopiuj
modelBuilder.Entity<IdentityUser>().HasKey(x => x.id) 

aby zdefiniować te klucze to EF wariuje i tworzy nową tabele ApplicationUsers a pozniej i tak leci exception

SZ
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1591
0

To nie dodawaj tego. To powinno być z automatu keyem.

UL
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 37
0

@szydlak: Ale jakimś cudem nie jest. Jak widać po stack trace to jeden z kilku.

SZ
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1591
0

Pokaż cały DbContenxt

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.