Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单数据插入复杂对象_C#_Insert_Foreign Keys_Simple.data - Fatal编程技术网

C# 简单数据插入复杂对象

C# 简单数据插入复杂对象,c#,insert,foreign-keys,simple.data,C#,Insert,Foreign Keys,Simple.data,这是我使用Simple.Data的第一天。我正在从纯ADO.NET迁移我的项目 我有一个带有列的订单表: Id CreationDate CustomerId -> FK to Customer table 以及我的项目中的Order类: int Id DateTime CreationDate Customer Customer 要向数据库插入新订单,我正在执行以下操作: var newOrder=... _db.Orders.Insert(new {

这是我使用Simple.Data的第一天。我正在从纯ADO.NET迁移我的项目

我有一个带有列的订单表:

Id
CreationDate
CustomerId  -> FK to Customer table
以及我的项目中的Order类:

int Id
DateTime CreationDate    
Customer Customer
要向数据库插入新订单,我正在执行以下操作:

var newOrder=...
_db.Orders.Insert(new {
                       CreationDate=newOrder.CreationDate,
                       CustomerId = newOrder.Customer.Id
                      }
                 );
有可能做得更简单些吗?对我来说更简单意味着不创建新的匿名对象和复制每个属性值。我知道这很简单。数据通过插入提供隐式转换,但当我尝试此操作时:

var newOrder=...
_db.Orders.Insert(newOrder);

我从SqlServer收到一个异常“CustomerId不能为NULL”。

客户ID不能为NULL。当CustomerId为null时,您需要不将数据插入数据库的逻辑

if (newOrder.Customer != null && newOrder.Customer.Id != null)
{
    _db.Orders.Insert(new 
    {
         CreationDate=newOrder.CreationDate,
         CustomerId = newOrder.Customer.Id
    });
}

客户ID不能为空。当CustomerId为null时,您需要不将数据插入数据库的逻辑

if (newOrder.Customer != null && newOrder.Customer.Id != null)
{
    _db.Orders.Insert(new 
    {
         CreationDate=newOrder.CreationDate,
         CustomerId = newOrder.Customer.Id
    });
}

我认为目前还不可能,但Mark正在研究v2的“允许通过一次调用保存对象图,如果可能的话”(参见他的博客文章:,尤其是“Better Wistuff”一段)

但是,您可以通过将“newOrder”对象转换为动态对象并简单地向其“添加”CustomerId值来避免复制每个属性值,如下所示:

var d = newOrder.ToDynamic();
d.CustomerId = newOrder.Customer.Id;
_db.Orders.Insert(d);
其中“ToDynamic()”是一个简单的扩展方法:

public static dynamic ToDynamic(this object @this)
{
    IDictionary<string, object> expando = new ExpandoObject();

    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(@this.GetType()))
    {
        expando.Add(property.Name, property.GetValue(@this));
    }

    return expando as ExpandoObject;
}
public静态动态到动态(this object@this)
{
IDictionary expando=新的ExpandoObject();
foreach(TypeDescriptor.GetProperties(@this.GetType())中的PropertyDescriptor属性)
{
expando.Add(property.Name,property.GetValue(@this));
}
返回expando作为expandooobject;
}

很简单。数据在插入期间将忽略原始的“Customer”属性,因为基础表中没有用于此的列。

我认为目前不可能,但Mark正在研究v2的“允许通过单个调用保存对象图,如果确实可能的话”(请参阅他的博文:,尤其是“更好地使用凝灰岩”一段)

但是,您可以通过将“newOrder”对象转换为动态对象并简单地向其“添加”CustomerId值来避免复制每个属性值,如下所示:

var d = newOrder.ToDynamic();
d.CustomerId = newOrder.Customer.Id;
_db.Orders.Insert(d);
其中“ToDynamic()”是一个简单的扩展方法:

public static dynamic ToDynamic(this object @this)
{
    IDictionary<string, object> expando = new ExpandoObject();

    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(@this.GetType()))
    {
        expando.Add(property.Name, property.GetValue(@this));
    }

    return expando as ExpandoObject;
}
public静态动态到动态(this object@this)
{
IDictionary expando=新的ExpandoObject();
foreach(TypeDescriptor.GetProperties(@this.GetType())中的PropertyDescriptor属性)
{
expando.Add(property.Name,property.GetValue(@this));
}
返回expando作为expandooobject;
}

Simple.Data将在插入过程中忽略原始的“Customer”属性,因为基础表没有此列。

您误解了我的问题。Customer&&Customer.Id肯定不是空的。我在问,Simple.Data是否可以匹配[Order]。[CustomerId]插入时使用Customer.Id属性的列。例如,通过[Order].[CustomerId]和[Customer].[Id]列之间的外键(就像从数据库中选择数据时那样),您误解了我的问题。Customer&&Customer.Id肯定不是null。我是在问,是否简单。数据可以匹配[Order].[CustomerId]插入时使用Customer.Id属性的列。例如,通过[Order].[CustomerId]和[Customer].[Id]列之间的外键(就像从数据库中选择数据时那样)谢谢。我刚刚静态地将CustomerId属性添加到我的Order类中,但您使用ToDynamic的解决方案更干净。展望简单。Data v2。谢谢您。我刚刚静态地将CustomerId属性添加到我的Order类中,但您使用ToDynamic的解决方案更干净。展望简单。Data v2。