【发布时间】:2017-08-14 21:42:02
【问题描述】:
我想获得一些关于“表达”的信息。
有两个类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Image Photo { get; set; }
public virtual ICollection<Image> UserGallery { get; set; }
}
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
public int Size { get; set; }
}
和
static void Main(string[] args)
{
Expression<Func<User, object>> ex1 = c => c.Name,
ex2 = c => c.Photo,
ex3 = c => c.UserGallery;
DetectPropertyType(ex1);//i want to print: 'scalar'
DetectPropertyType(ex2);//i want to print: 'related'
DetectPropertyType(ex3);//i want to print: 'collection'
}
public static void DetectPropertyType(Expression<Func<User, object>> expression)
{
//How to detect kind of 'expression'
//my question here
}
我想检测,因为当我想要 Update 和 Object 时,它有一个参考;我将使用其他代码处理它。
好的
myContext.Entry(AttachedObject).Property(ex1).IsModified = true;
错误:“用户”类型的属性“照片”不是原始类型或 复杂的属性。 Property 方法只能与原语一起使用 或复杂的属性。使用引用或集合方法。
myContext.Entry(AttachedObject).Property(ex2).IsModified = true;
错误
myContext.Entry(AttachedObject).Property(ex3).IsModified = true;
【问题讨论】:
-
我认为你必须按照它所说的去做,使用
Reference或Collection而不是Property。喜欢这个myContext.Entry(AttachedObject).Reference(ex3).IsModified = true;。Reference仅用于简单属性,Reference处理单个导航属性,Collection处理集合。 -
是的,我知道,但首先需要检测
-
Entry(obj).Reference(ex3).EntityEntry.State = System.Data.Entity.EntityState.Modified;
-
啊,我明白了,您希望能够以编程方式判断您是否需要使用
Property、Reference或Collection?如果是这样,您应该更新您的问题以更清楚一点。我会看看我是否能找到任何信息如何做到这一点。
标签: c# entity-framework expression