Entity framework 如何使用实体框架插入多个值。。?
我将Silverlight5与MVVM框架和实体框架一起使用。在我的项目中,我有一个疑问Entity framework 如何使用实体框架插入多个值。。?,entity-framework,mvvm,silverlight-5.0,Entity Framework,Mvvm,Silverlight 5.0,我将Silverlight5与MVVM框架和实体框架一起使用。在我的项目中,我有一个疑问 Customer_Entity ocustomerEntity=new Customer_Entity (); ocustomerEntity.CustomerID=Customer_ID; ocustomerEntity.EntityTypeID =1; . . . . ocustomerEntity.CreatedTime
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
我有一个名为“客户”的实体,其结构如下所示
Customer ocustomer = new Customer();
ocustomer.CustomerName = customername;
ocustomer.CompanyName = company;
ocustomer.AddressLine1 = address1;
ocustomer.AddressLine2 = address2;
ocustomer.City = city;
ocustomer.State = state;
ocustomer.Country = country;
ocustomer.ZipCode = zipcode;
ocustomer.Notes = note;
_context.Customers.Add(ocustomer);
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
现在我需要将Customer值插入另一个名为Customer\u Entity的表中
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
在这里,我需要在每一行中向customer_实体插入customer value。。
客户_实体表结构如下:
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
EntityID| CustomerID| EntityValue|
----------------------------------
1 | 22 |jasper |
2 | 22 |Company:Raj |
3 | 22 |Address |
ocustomer.CustomerName=customername
.
.
.
ocustomer.CreatedTime=system.DateTime.Now..
因此,我需要使用唯一的CustomerID在每一行中插入所有值。。
需要帮助来解决这个问题 在
CustomerEntity
对象上,应该有一个指向相应Customer
记录的导航属性。类似地,我希望您的Customer
类上也有CustomerEntity
的集合
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
在这种情况下,您应该实例化Customer
对象,并填充必要的信息。不过,还不要将其添加到DbSet中。然后,创建所有需要的CustomerEntity
记录,通过使用导航属性本身(即不是ID字段)将其连接到Customer
对象,并将该CustomerEntity
记录添加到上下文类中相应的DbSet中
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;
最后一步,将
Customer
对象添加到相应的数据库集中,然后运行SaveChanges。你该走了。实体框架应该自动为您生成ID并将其填充到CustomerEntity记录中。我认为问题在于您的模型。你是如何建模的?你用过FLUEN API吗?没有FLUEN API。。我正在使用实体框架
Customer_Entity ocustomerEntity=new Customer_Entity ();
ocustomerEntity.CustomerID=Customer_ID;
ocustomerEntity.EntityTypeID =1;
.
.
.
.
ocustomerEntity.CreatedTime =System.DateTime.Now;
ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid;