Страницы

Поиск по вопросам

среда, 4 марта 2020 г.

Fluent API связь один к одному

#c_sharp #net #entity_framework


Не получается сделать связь один к одному, ошибка : 


  System.InvalidOperationException: 'Unable to determine the principal end of an
association between the types 'DataBase.Entities.Branch' and 'DataBase.Entities.Address'.
The principal end of this association must be explicitly configured using either the
relationship fluent API or data annotations.'


public class Branch
{
    public int Id { get; set; }
    public string Metro { get; set; }
    public string Name { get; set; }
    public string CommentToAddress { get; set; }
    public string Schedule { get; set; }
    public string SchedulePrivatePerson { get; set; }
    public string ScheduleGeneral { get; set; }
    public string ScheduleEntities { get; set; }
    public Bank Bank { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public string CountryCity { get; set; } //город
    public string StreetName { get; set; } // улица
    public string StreetType { get; set; } // "тип" улицы(улица, проспект, проезд)
- для оптимизации запроса к карте
    public string ClarifyingAddress { get; set; } //адрес после улицы
    public Branch Branch { get; set; }
}

 public class Bank
{
    public Bank()
    {
        Branches = new HashSet();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public float DollarBuy { get; set; }
    public float DollarSell { get; set; }
    public float EuroBuy { get; set; }
    public float EuroSell { get; set; }
    public System.DateTime UpdateTime { get; set; }
    public /*virtual*/ ICollection Branches { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Value { get; set; }
    public string Name { get; set; }
}


Configs

internal class  AddressConfig : EntityTypeConfiguration
{ public AddressConfig(DbModelBuilder modelBuilder) { HasKey(p => p.Id); Property(p => p.CountryCity).IsRequired(); Property(p => p.StreetName).IsRequired(); Property(p => p.StreetType).IsRequired(); Property(p => p.ClarifyingAddress).IsRequired(); } } internal class BankConfig : EntityTypeConfiguration { internal BankConfig(DbModelBuilder modelBuilder) { HasKey(p => p.Id); Property(p => p.Name).IsRequired(); Property(p => p.DollarBuy).IsRequired(); Property(p => p.DollarSell).IsRequired(); Property(p => p.EuroBuy).IsRequired(); Property(p => p.EuroSell).IsRequired(); Property(p => p.UpdateTime).IsRequired().HasColumnType("datetime"); modelBuilder.Entity().HasMany(p => p.Branches).WithRequired(p=>p.Bank); } } internal class BranchConfig : EntityTypeConfiguration { internal BranchConfig(DbModelBuilder modelBuilder) { HasKey(p => p.Id); Property(p => p.Metro).IsOptional(); Property(p => p.Name).IsOptional(); Property(p => p.CommentToAddress).IsOptional(); Property(p => p.CommentToAddress).IsOptional(); Property(p => p.SchedulePrivatePerson).IsOptional(); Property(p => p.ScheduleGeneral).IsOptional(); Property(p => p.ScheduleEntities).IsOptional(); Property(p => p.Schedule).IsOptional(); modelBuilder.Entity() .HasRequired(p => p.Bank) .WithMany(p => p.Branches); modelBuilder.Entity
() .HasRequired(p => p.Branch) .WithRequiredPrincipal(p => p.Address); } } class CityConfig: EntityTypeConfiguration { public CityConfig() { HasKey(p => p.Id); Property(p => p.Value).IsOptional(); Property(p => p.Name).IsOptional(); } } Context public class Context : DbContext { public Context():base("DbMap") { } static Context() { System.Data.Entity.Database.SetInitializer(new ContextInitializer()); } public DbSet Banks { get; set; } public DbSet Branches { get; set; } public DbSet Cities { get; set; } public DbSet
Addresses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new BankConfig(modelBuilder)); modelBuilder.Configurations.Add(new BranchConfig(modelBuilder)); modelBuilder.Configurations.Add(new AddressConfig(modelBuilder)); modelBuilder.Configurations.Add(new CityConfig()); } ConnectionString


Ответы

Ответ 1



Что-то вы перемудрили, для того что бы создать связь "один-к-одному" достаточно указать: modelBuilder.Entity
() .HasRequired(p => p.Branch) .WithRequiredPrincipal(p => p.Address); Поправил конфиги: public class AddressConfig : EntityTypeConfiguration
{ public AddressConfig() { Property(p => p.CountryCity).IsRequired(); Property(p => p.StreetName).IsRequired(); Property(p => p.StreetType).IsRequired(); Property(p => p.ClarifyingAddress).IsRequired(); HasRequired(p => p.Branch) .WithRequiredPrincipal(p => p.Address); } } public class BankConfig : EntityTypeConfiguration { public BankConfig() { Property(p => p.Name).IsRequired(); Property(p => p.DollarBuy).IsRequired(); Property(p => p.DollarSell).IsRequired(); Property(p => p.EuroBuy).IsRequired(); Property(p => p.EuroSell).IsRequired(); Property(p => p.UpdateTime).IsRequired().HasColumnType("datetime"); HasMany(p => p.Branches).WithRequired(p => p.Bank); } } public class BranchConfig : EntityTypeConfiguration { public BranchConfig() { HasRequired(p => p.Bank) .WithMany(p => p.Branches); } }

Комментариев нет:

Отправить комментарий