Entity Framework adds a null empty row before the actually row, when adding test data (a reservation)
<p>I have a problem that when adding a reservation, or my test data to the database (using Entity Framework), it created an empty null row in one of the tables.</p>

<p>I have 3 tables</p>

<ul>
<li>Reservation</li>
<li>ContactPerson</li>
<li>Rooms</li>
</ul>

<p>There is a many to many relationship between <code>Reservation</code> and <code>Rooms</code>. There is a one to many between <code>ContactPerson</code> and <code>Reservation</code>.</p>

<p>I'm using the code-first approach.</p>

<p><strong>Reservation</strong> class:</p>

<pre><code>public class Reservation
{
    public Reservation()
    {
        Room = new List&lt;Room&gt;();
        ContactPerson = new ContactPerson();
    }

    public int ReservationID { get; set; }
    public string ReservationNumber { get; set; }
    public DateTime CheckInDate { get; set; }
    public DateTime CheckOutDate { get; set; }
    public int ContactPersonID { get; set; }

    public virtual ContactPerson ContactPerson { get; set; }
    public virtual ICollection&lt;Room&gt; Room { get; set; }
}
</code></pre>

<p><strong>Room</strong> class:</p>

<pre><code>public class Room
{
    public int ID { get; set; }
    public string RoomNumber { get; set; }
    public bool Occupied { get; set; }
    public bool Smoking { get; set; }
    public bool Minibar { get; set; }
    public int Beds { get; set; }
    public RoomTypeEnum? RoomType { get; set; }

    public virtual ICollection&lt;Reservation&gt; Reservations { get; set; }
}

public enum RoomTypeEnum
{
    Single = 0, Double = 1, Family = 2, Suite = 3
}
</code></pre>

<p><strong>ContactPerson</strong> class:</p>

<pre><code>public class ContactPerson
{
    public int ContactPersonID { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public Nationality NationalityType { get; set; }

    public virtual ICollection&lt;Reservation&gt; Reservations { get; set; }
}

public enum Nationality
{
    Denmark, Germany, England, France, Holland, Belgium, Norway, Sweden, Finland
}
</code></pre>

<p>My <code>DataContext</code> class:</p>

<pre><code>public interface IDataContext
{
    DbSet&lt;Room&gt; Rooms { get; }
    DbSet&lt;Reservation&gt; Reservations { get; }
    DbSet&lt;ContactPerson&gt; ContactPersons { get; }
    int SaveChanges();
    void MarkAsModified(Reservation item);
    void MarkRoomAsModified(Room item);
    void CreateReservation(Reservation item);
}

public class DataContext : DbContext, IDataContext
{
    public DataContext() : base("DataContext")
    {

    }

    // DbSet to bookings
    public DbSet&lt;Reservation&gt; Reservations { get; set; }
    public DbSet&lt;ContactPerson&gt; ContactPersons { get; set; }
    public DbSet&lt;Room&gt; Rooms { get; set; }

    public void MarkAsModified(Reservation item)
    {
        Entry(item).State = EntityState.Modified;
    }

    public void MarkRoomAsModified(Room item)
    {
        Entry(item).State = EntityState.Modified;
    }

    public void CreateReservation(Reservation item)
    {
        Reservations.Add(item);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();

        modelBuilder.Entity&lt;Reservation&gt;()
            .HasMany(c =&gt; c.Room).WithMany(i =&gt; i.Reservations)
            .Map(t =&gt; t.MapLeftKey("ReservationID")
            .MapRightKey("RoomID")
            .ToTable("RoomReservation"));
    }
}
</code></pre>

<p>I'm inserting test data with the <code>Configuration</code> class that is being generated when you use migrations. </p>

<p><strong>Configuration:</strong></p>

<pre><code>protected override void Seed(WebApplication1.DAL.DataContext context)
{
        var reservations = new List&lt;Reservation&gt;
            {
                new Reservation{ReservationNumber = "123456789", CheckInDate = DateTime.Parse("2016-01-01"), CheckOutDate = DateTime.Parse("2016.01.15"), ContactPersonID = 1
                }
            };

        reservations.ForEach(b =&gt; context.Reservations.AddOrUpdate(r =&gt; r.ReservationID, b));
        context.SaveChanges();

        var contactPersons = new List&lt;ContactPerson&gt;
            {
                new ContactPerson{Title = "Mr" ,FirstName = "Anders",LastName = "Christensen", Email = "AndersChristensen@gmail.com", NationalityType = Nationality.Denmark, PhoneNumber = "44216731" }
            };

        contactPersons.ForEach(b =&gt; context.ContactPersons.AddOrUpdate(c =&gt; c.ContactPersonID, b));
        context.SaveChanges();

        var rooms = new List&lt;Room&gt;
        {
            new Room {RoomNumber = "101", Beds = 1, RoomType = RoomTypeEnum.Single, Minibar = true, Occupied = true, Smoking = false},
            new Room {RoomNumber = "102", Beds = 1, RoomType = RoomTypeEnum.Single, Minibar = true, Occupied = true, Smoking = false},
            new Room {RoomNumber = "103", Beds = 1, RoomType = RoomTypeEnum.Single, Minibar = true, Occupied = false, Smoking = false},
            new Room {RoomNumber = "104", Beds = 2, RoomType = RoomTypeEnum.Double, Minibar = true, Occupied = false, Smoking = true},
            new Room {RoomNumber = "105", Beds = 3, RoomType = RoomTypeEnum.Family, Minibar = true, Occupied = false, Smoking = false},
        };

        rooms.ForEach(b =&gt; context.Rooms.AddOrUpdate(b));
        context.SaveChanges();

        AddOrUpdateReservation(context, "101", "123456789");
        context.SaveChanges();
    }

    void AddOrUpdateReservation(DataContext context, string roomNumber, string reservationNumber)
    {
        var reservation = context.Reservations.SingleOrDefault(c =&gt; c.ReservationNumber == reservationNumber);
        var room = reservation.Room.SingleOrDefault(r =&gt; r.RoomNumber == roomNumber);
        if (room == null)
            reservation.Room.Add(context.Rooms.Single(i =&gt; i.RoomNumber == roomNumber));
    }
</code></pre>

<p>The <code>Reservation</code> table looks good, the <code>Rooms</code> table looks good, too, and the shared table <code>ReservationRoom</code> also looks good. The problem is the <code>ContactPerson</code> table, when I add a room or reservation, it will add an empty row before added the right contact person. </p>

<p>You see it in this screenshot:</p>

<p><a href="https://i.stack.imgur.com/n1c3Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n1c3Q.png" alt="enter image description here"></a></p>

<p>Here you see the result just after running commands, <code>add-migration InitialCreate</code> and <code>update-database</code>.</p>

<p>This also happens when trying to add a reservation I have create in my application, when using the method:</p>

<pre><code>public void CreateReservation(Reservation item)
{
    Reservations.Add(item);
}
</code></pre>

<p>The reservation table look like this:</p>

<p><a href="https://i.stack.imgur.com/yUWPp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yUWPp.png" alt="enter image description here"></a></p>

<p>Here you can see it points to the empty null row that has been created for some reason. </p>

<p>Hope someone can tell me what could be wrong here. I have used days trying to reconfigure the models and stuff, but none have seem to worked for me.</p>