【问题标题】:how to insert multiple values using entity framework..?如何使用实体框架插入多个值..?
【发布时间】:2013-02-21 12:36:04
【问题描述】:

我将 Silverlight5 与 MVVM 框架和实体框架一起使用。在我的项目中,我有一个疑问..

我有一个名为“客户”的实体,其结构如下..

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 值插入另一个名为 Customer_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_Entity.. Customer_Entity 表结构如下,

EntityID| CustomerID| EntityValue|
----------------------------------                                                                                                 
1       | 22        |jasper      |                                                                                  
2       | 22        |Company:Raj |
3       | 22        |Address     |

ocustomer.CustomerName=customername
.
.
.
ocustomer.CreatedTime=system.DateTime.Now..

所以我需要使用唯一的 CustomerID 在每一行中插入所有值。 需要帮助来解决这个问题..

【问题讨论】:

  • 问题是,我认为,你的模型。你是如何建模的?你用过 FLUEN API 吗?
  • 没有 Fluen API.. 我正在使用实体框架

标签: entity-framework mvvm silverlight-5.0


【解决方案1】:

在您的CustomerEntity 对象上,您应该有一个导航属性指向相应的Customer 记录。同样,我希望您的 Customer 类也有 CustomerEntity 的集合。

在这种情况下,您应该实例化Customer 对象,并填充必要的信息。不过,不要将它添加到 DbSet。然后,创建您需要的所有 CustomerEntity 记录,使用导航属性本身(即 NOT ID 字段)将其连接到 Customer 对象,并添加 CustomerEntity记录到你的 Context 类中对应的 DbSet。

作为最后一步,将您的 Customer 对象添加到相应的 DbSet,然后运行 ​​SaveChanges。你应该很高兴。实体框架应自动为您生成 ID 并将其填充到 CustomerEntity 记录。

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多