У меня есть две таблицы, в которых я хочу реализовать отношение 1-1. Я сталкиваюсь с проблемой, описанной в этом посте: EntityFramework: неверное имя столбца * _ID1 Я попытался реализовать решение, чтобы добавить атрибут Внешнего ключа, и я столкнулся с другой проблемой:
Множественность недопустима в роли Account_Companies_Target в отношении Account_Companies. Поскольку свойства зависимой роли не являются ключевыми свойствами, верхняя граница кратности зависимой роли должна быть '*'.
Это классы:
public class Account
{
[Key]
public Guid Id { get; set; }
public string CompanyName { get; set; }
public long CNP { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public long PhoneNumber { get; set; }
public string Address { get; set; }
[Required]
public string UserName { get; set; }
[Required(ErrorMessage = "Password is Required.")]
public string Password { get; set; }
public DateTime CreatedOn { get; set; }
[NotMapped]
[Required(ErrorMessage = "Confirmation Password is Required")]
[Compare("Password", ErrorMessage = "Password Must Match")]
public string ConfirmPassword { get; set; }
[Required]
public AccountType AccountType { get; set; }
public int CUI { get; set; }
[ForeignKey("Id")]
public virtual Company Companies { get; set; }
}
public class Company
{
[Key]
public long CUI { get; set; }
public Guid Id { get; set; }
public string CompanyName { get; set; }
public string Simbol { get; set; }
public int SharesCount { get; set; }
public decimal SharePrice { get; set; }
public virtual Account Account { get; set; }
}
и это переопределение OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>()
.HasOptional(account => account.Companies)
.WithRequired(company => company.Account);
}