C# 插入时列名无效-实体框架
我首先使用实体框架使用代码。我在数据库中有3个表:C# 插入时列名无效-实体框架,c#,sql-server,entity-framework,ef-code-first,C#,Sql Server,Entity Framework,Ef Code First,我首先使用实体框架使用代码。我在数据库中有3个表: 放置 Profil PlaceMember 这是一种多对多的关系。一个地方可以有多个profil作为成员,profil可以是多个地方的成员。PlaceMember是关系表。 地点代码: [Table("Place")] public partial class PlaceDTO { public PlaceDTO() { Members = new HashSet<ProfilDTO>();
- 放置
- Profil
- PlaceMember
[Table("Place")]
public partial class PlaceDTO
{
public PlaceDTO()
{
Members = new HashSet<ProfilDTO>();
}
public int id { get; set; }
[StringLength(100)]
public string description { get; set; }
public DateTime dateOut { get; set; }
public DateTime createDate { get; set; }
public int? canceled { get; set; }
public int profilId { get; set; }
public int addressId { get; set; }
public virtual AddressDTO Address { get; set; }
public virtual ProfilDTO Profil { get; set; }
public virtual ICollection<ProfilDTO> Members { get; set; }
}
在数据库中,placeId和invitedId都是表的标识键。
当我试图向place表添加一个新位置时,我发现PlaceDTO_id不是有效的列名,尽管数据库中的place表有一个名为id的列,并且是标识列
context.Address.Attach(place.Address);
context.Address.Add(place.Address);
place.addressId = place.Address.id;
context.Places.Attach(place);
context.Places.Add(place);
context.SaveChanges();
place对象put in param包含Profil表格中存在的3个成员。
当我从PlaceDTO表中删除Members字段时,将插入该位置,但不会向PlaceMember表中添加任何内容
怎么了?我看不出你的连接表在哪里发挥作用。您是否有一些流畅的代码来配置关系、MapLeftKey、MapRightKey、ToTable(“PlaceMember”)等?您根本不需要表PlaceMember。EF将自动生成它。但如果您仍然希望手动创建它,则应该在两个类中创建
ICollection PlaceMembers
,并在表PlaceMember
中创建对此表的引用(作为外键-复合主键)。我甚至不知道您的连接表在哪里发挥作用。您是否有一些流畅的代码来配置关系、MapLeftKey、MapRightKey、ToTable(“PlaceMember”)等?您根本不需要表PlaceMember。EF将自动生成它。但如果仍要手动创建它,则应在这两个类中创建ICollection PlaceMembers
,并在表PlaceMember
中创建对此表的引用(作为外键-复合主键)。
[Table("PlaceMember")]
public partial class PlaceMemberDTO
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int placeId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int invitedId { get; set; }
}
context.Address.Attach(place.Address);
context.Address.Add(place.Address);
place.addressId = place.Address.id;
context.Places.Attach(place);
context.Places.Add(place);
context.SaveChanges();