다음과 같이 Organization 클래스와 User 클래스가 있습니다. & 두 개의 개별적인 many-to-many를 필요로하지 않고 각 개별적으로 Address를 할당 할 수 있기를 원합니다. AddressUser에 대한 테이블 & AddressOrganization, AddressAnything, 등등. 이유는 내가 주소를 할당하고자하는 여러 엔티티가 &에있을 때 주소 레코드가 필요한 모든 단일 엔티티에 대한 중간 조인 테이블을 원하지 않기 때문입니다.일반 오브젝트 유형의 Fluent API 복합 외래 키
관련 주소가 필요한 각 엔티티에 대해 Address.OrganizationId 또는 Address.UserId와 같은 외래 키를 저장하고 싶지 않습니다.
Fluent API 또는 Code-First Data Annotation은 객체 유형이있는 일종의 복합 일반 외래 키를 수용 할 수 있습니까?
DomainObjectType 테이블과 비슷합니까?
DomainObjectType :
public class DomainObjectType
{
[Key]
public int Id { get; set; } // seeded
public string ObjectType { get; set; } // User or Organization
}
DomainObjectType 함께 시드 할 것 :
var objTypes = new List<DomainObjectType>
{
new DomainObjectType() { Id = 1, ObjectType = "User" },
new DomainObjectType() { Id = 2, ObjectType = "Organization" }
};
objTypes.ForEach(c => context.DomainObjectTypes.Add(c));
context.SaveChanges();
조직 :
public class Organization
{
// Primary key
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
#region Navigation Properties
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
#endregion
}
사용자
public class User
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int OrganizationId { get; set; }
[ForeignKey("OrganizationId")]
public virtual Organization Organization { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
주소
public class Address
{
// Primary Key
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string AddressType { get; set; }
#region Foreign Keys
// not completely sure about this? (maybe move to many-to-many "join" table)
public int DomainObjectId { get; set; } // Composite key; would be either an OrganizationId or UserId
public string DomainObjectTypeId { get; set; } // Composite key
#endregion
#region Navigation Properties
public virtual ICollection<Organization> Organizations { get; set; }
public virtual ICollection<User> Users { get; set; }
#endregion
}
유창함 API 물건 :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention then the generated tables will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// one-to-many Organization-to-Users
modelBuilder.Entity<User>()
.HasRequired(u => u.Organization)
.WithMany(o => o.Users)
.HasForeignKey(u => u.OrganizationId);
// what goes here for generic AddressDomainObjectType?
}
'Address'와'User'와'Address'와'Organization' 사이에 다 대다 관계가 있습니까? 처음에는 그렇게 말했지만 모델 스케치는 다 대다 (many-to-many)처럼 보이지 않습니다. – Slauma
당신의 모델은 @Slauma가 말한 것처럼 혼란 스럽 습니다만, 여러분의'Address' 테이블에 대해 다음과 같은'.HasForeignKey (i => new {i.DomainObjectId, i.DomainObjectTypeId})'가 필요할지도 모릅니다. – NSGaga
내 사용자 및 주소 클래스를 수정하여 다 대다를 나타냅니다. 그것은 결국 many - to - many를 필요로 할 것 같습니다 - AddressDomainObjectType? – woodyiii