我有两个Class Customers和MembershipType,并且有一个结构。创建数据库时,MembershipType列的值为null。我该如何解决?
## Guess the Right Number ##
#!/bin/bash
clear
while :
do
echo "enter the guessing number:"
read num
if [ $num -eq 47 ]; then
echo "That is a right number"
break
elif [ $num -le 47 ]; then
echo "print higher number"
elif [ $num -ge 47 ]; then
echo "print lower number"
fi
done
客户表
public class Customer
{
public int CustomerId { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
public string CustomerGender { set; get; }
public byte Age { set; get; }
public bool IsSubscribedToNewsLetter { set; get; }
public MembershipType MembershipType{ get; set; }
public byte MembershipTypeId { set; get; }
}
public class MembershipType
{
[Key]
public byte MemebershipId { set; get; }
public short SignUpFee{ get; set; }
public byte DurationInMonth { set; get; }
public byte DiscountRate { get; set; }
}
迁移:
CusId CustName CusGender Age IsSub.. MembershipTypeId MemberShipType_Membershipid
1 Jobin Male 27 0 2 NULL
答案 0 :(得分:0)
我看到了几个可能的问题。
尝试一致的拼写... 客户具有“MembershipTypeId”外键,但“MembershipType”的主键是“MemembershipId”。 如果您使用MembershipType MembershipTypeId的密钥会发生什么? EF的基于约定的配置会变得混乱吗?
EF并不真正喜欢一对一的关系...当它创建它们时,它希望依赖的主键与主http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx的主键具有相同的值
答案 1 :(得分:0)
将MembershipTypeId
和Customer
中MembershipType
字段的类型更改为int
,删除现有迁移并再次添加新迁移,然后选择{ {1}}字段作为外键
否则,您必须在Context类
中使用Fluent API声明关系答案 2 :(得分:0)
您可以按如下方式解决问题:在[Required]
上方MembershipTypeId
以及[ForeignKey("MembershipTypeId")]
属性上方MembershipType
public class Customer
{
public int CustomerId { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
public string CustomerGender { set; get; }
public byte Age { set; get; }
public bool IsSubscribedToNewsLetter { set; get; }
[Required]
public byte MembershipTypeId { set; get; }
[ForeignKey("MembershipTypeId")]
public MembershipType MembershipType { get; set; }
}
public class MembershipType
{
[Key]
public byte MemebershipId { set; get; }
public short SignUpFee { get; set; }
public byte DurationInMonth { set; get; }
public byte DiscountRate { get; set; }
}
最好删除此迁移并创建一个新的,因为实体可能会丢失。