实体框架中不允许多重性? [英] Multiplicity not allowed in Entity Framework?
问题描述
我正在建立一个基本数据库,但是我得到以下异常。
Bracelet_Guest_Source ::多重性在关系'Bracelet_Guest'中的角色'Bracelet_Guest_Source'中无效。由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为'*'。
模型看起来像:
I am making a basic data base but I am getting the following exception.
Bracelet_Guest_Source: : Multiplicity is not valid in Role 'Bracelet_Guest_Source' in relationship 'Bracelet_Guest'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
The Models looks like:
public class Bracelet
{
[Key]
public int BraceletID { get; set; }
public string BraceletNumber { get; set; }
//Guest
[ForeignKey("Guest")]
public int GuestID { get; set; }
public virtual Guest Guest { get; set; }
//Restaurant Entries
public virtual RestaurantEntry RestaurantEntry { get; set; }
}
public class Guest
{
public Guest()
{
}
[Key]
public int GuestID { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public decimal Age { get; set; }
public string Nationality { get; set; }
public string Pin { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime DateOfArrival { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime DateOfDeparture { get; set; }
public int RoomNumber { get; set; }
public string AllInclusiveType { get; set; }
//[ForeignKey("Bracelet")]
//public int BraceletID { get; set; }
public virtual Bracelet Bracelet { get; set; }
}
public class RestaurantEntry
{
private ICollection<Bracelet> bracelets;
public RestaurantEntry()
{
this.bracelets = new HashSet<Bracelet>();
}
[Key]
public int RestaurantEntryID { get; set; }
//[ForeignKey("Bracelet")]
//public int BraceletID { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime Time { get; set; }
//Bracelet
public virtual ICollection<Bracelet> Bracelets
{
get { return bracelets; }
set { this.bracelets = value; }
}
}
关系就像这样
手镯 - 来宾 - 1对1
手镯 - 餐厅Enry - 1对多
但是无法弄清楚什么是例外。
The relationships are like this
Bracelet - Guest - 1 to 1
Bracelet - Restaurant Enry - 1 to many
But cant figure out what is the exception.
推荐答案
这是因为你的模特有一些问题。
首先,我认为您的数据模型存在根本问题。如果手镯和客人是1-1,那么推断客人永远不能更换手镯,这是糟糕的设计并且稍后会咬你,如果客人需要更换他们的手镯(以后再回来等)。如果您的业务规则不可能,请通过向Guest对象添加BraaceletNumber属性并使用Guest作为您的关系来合并这两个对象。
在当前代码下但是,您需要对访客执行此操作:
This is because you have some issues in your models.
Firstly, I think you have a fundamental issue with your data model. If bracelets and Guests are 1-1, that infers that a guest can never change bracelets, which is bad design and will bite you later, should a guest need to replace their bracelet (coming back at a later date, etc.). If that's not possible in your business rules, merge the two objects by adding a BraaceletNumber property to the Guest object and using the Guest as your relationship.
Under the current code, though, you need to do this to Guest:
public class Guest
{
...
// Commenting this breaks EF
[ForeignKey("Bracelet")]
public int BraceletID { get; set; }
public virtual Bracelet Bracelet { get; set; }
}
这样做到手镯:
And do this to Bracelet:
public class Bracelet
{
...
//Guest
[ForeignKey("Guest")]
public int GuestID { get; set; }
public virtual Guest Guest { get; set; }
// This needs to be an ICollection for 1-M
public virtual ICollection<RestaurantEntry> RestaurantEntry { get; set; }
}
这样做到RestaurantEntry:
And do this to RestaurantEntry:
public class RestaurantEntry
{
...
[ForeignKey("Bracelet")]
public int BraceletID { get; set; }
...
//Bracelet - Should be singular, each entry points to 1 bracelet
public virtual Bracelet Bracelets { get; set; }
}
这篇关于实体框架中不允许多重性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!