简体   繁体   中英

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

I am using codefirst technique in my MVC ASP.NET application to generate my database. I attempted to Create a one to many relationship between two of my tables, Customer and Room.

I want one customer to be able to book one or more than one room.

The Following is my Customer model:

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; }
}

And the following is model

    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; }

}

Let's say when ever I pass a Customer object to my database, entity frameworks creates new Room records even when I define the RoomID for the relevant customer entries.

Let me elaborate, let's say I run the following code:

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();

Or even when I don't define any other parameter except the room id in my variable b, I get additional records created in my Rooms table in my database.

Even when I choose RoomID as 1, it will create a new record with a new RoomID with other fields the same as I define them. Any help?

If all you are trying to do is add a Customer to an existing Room, you could do something like this.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM