Insert data using Entity Framework model

后端 未结 4 894
感情败类
感情败类 2020-11-27 03:07

I\'m trying to insert some data in my database using Entity Framework model, but for some unknown reasons to me, it does nothing.

Am I missing something here?

<
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:01

    I'm using EF6, and I find something strange,

    Suppose Customer has constructor with parameter ,

    if I use new Customer(id, "name"), and do

     using (var db = new EfContext("name=EfSample"))
     {
        db.Customers.Add( new Customer(id, "name") );
        db.SaveChanges();
     }
    

    It run through without error, but when I look into the DataBase, I find in fact that the data Is NOT be Inserted,

    But if I add the curly brackets, use new Customer(id, "name"){} and do

     using (var db = new EfContext("name=EfSample"))
     {
        db.Customers.Add( new Customer(id, "name"){} );
        db.SaveChanges();
     }
    

    the data will then actually BE Inserted,

    seems the Curly Brackets make the difference, I guess that only when add Curly Brackets, entity framework will recognize this is a real concrete data.

提交回复
热议问题