实体框架在一对多关系中在数据库中创建不需要的记录

Entity Framework Creates Unwanted Records In Database in one to many relationships

提问人:Asbah Qadeer 提问时间:7/14/2017 更新时间:7/14/2017 访问量:108

问:

我在 MVC ASP.NET 应用程序中使用 codefirst 技术来生成数据库。我尝试在我的两个表(Customer 和 Room)之间创建一对多关系。

我希望一位客户能够预订一个或多个房间。

以下是我的客户模型:

public class Customer
{
    public int id { get; set; }

    [Display(Name = "Name")]
    public string name { get; set; }

    [Display(Name = "Email Address")]
    public string email { get; set; }

    [Display(Name = "Phone Number")]
    public string phoneno { get; set; }

    [Display(Name = "Date of Birth")]
    public DateTime DOB { get; set; }

    [Display(Name = "CNIC")]
    public string cnic { get; set; }

    public List<int> Roomsid { get; set; }


    [Display(Name = "Check In Date")]
    public DateTime? checkin { get; set; }

    [Display(Name = "Check Out Date")]
    public DateTime? checkout { get; set; }

    public List<Room> Room { get; set; }
}

以下是模型

    public class Room
{
    public int id { get; set; }

    public int floorno { get; set; }

    public string roomclass { get; set; }

    public int cost { get; set; }

    public string bedcount { get; set; }

    public string facilities { get; set; }

    public string description { get; set; }

    public int? Customerid { get; set; }

}

假设每当我将 Customer 对象传递到我的数据库时,实体框架都会创建新的 Room 记录,即使我为相关客户条目定义了 RoomID。

让我详细说明一下,假设我运行以下代码:

ApplicationDbContext x = new ApplicationDbContext();
        var a = new Customer() { };
        var b = new List<Room>() {
            new Room { id=2 ,floorno=2, bedcount="2", cost=2, description="2", facilities="2", roomclass="2" },
            new Room {id=3 ,floorno=3, bedcount="3", cost=2, description="3", facilities="3", roomclass="3" },
            new Room {id=4 ,floorno=4, bedcount="4", cost=4, description="4", facilities="4", roomclass="4" },
        };


        a.checkin = Convert.ToDateTime("05 Mar 2017");
        a.checkout = Convert.ToDateTime("07 Mar 2017");
        a.DOB = Convert.ToDateTime("02 Mar 2000");
        a.cnic = "asfsgwlkgnwe98hf0";
        a.email = "agjw98e98weg98";
        a.name = "Äfnan Makhdoom";
        a.Rooms = b;

        x.Customers.Add(a);
        x.SaveChanges();

或者,即使我没有在变量 b 中定义除房间 ID 之外的任何其他参数,我也会在数据库的 Rooms 表中创建其他记录。

即使我选择 RoomID 为 1,它也会创建一个具有新 RoomID 的新记录,其他字段与我定义的字段相同。有什么帮助吗?

C# 数据库 实体

评论

0赞 Afnan Makhdoom 7/14/2017
我自己一直想知道这一点!
0赞 Venkata Dorisala 7/14/2017
Rooms对象中的属性应为 。在这里看到类似的答案 stackoverflow.com/questions/42411810/...Customervirtual
0赞 Asbah Qadeer 7/14/2017
将其设置为虚拟根本没有区别
0赞 WiseGuy 7/14/2017
您是否尝试过添加房间并保存然后更新客户/房间关系?
0赞 WiseGuy 7/14/2017
此外,如果这些聊天室已存在,并且您正在尝试将客户添加到聊天室,则需要获取现有聊天室,而不是创建新聊天室。

答:

0赞 WiseGuy 7/14/2017 #1

如果您要做的只是将客户添加到现有聊天室,则可以执行类似操作。

    var a = new Customer() { };
    var roomWithId3 = x.Rooms.Single(x => x.Id == 3);

   a.rooms.Add(roomWithId3);
    x.Customers.Add(a);
    x.SaveChanges();

评论

0赞 Asbah Qadeer 7/14/2017
var b = new List<Room>() { vontex.Rooms.Single(n => n.id == 2), vontex.Rooms.Single(n => n.id == 3), vontex.Rooms.Single(n => n.id == 4) };a.房间 = bex;虽然我不得不这样做,但它奏效了!非常感谢