Is there a way to use `dynamic` in lambda expression tree?

后端 未结 3 913
感动是毒
感动是毒 2021-01-17 10:16

First, spec. We use MVC5, .NET 4.5.1, and Entity framework 6.1.

In our MVC5 business application we have a lot of repetitive CRUD code. My job is to \"automate\" mos

3条回答
  •  -上瘾入骨i
    2021-01-17 10:35

    If you are defining the BaseEntity class, you could add a virtual read only property that returns the actual ID property. I beleive EF treats read only properties as "computed", so they are not stored to the db.

    public abstract class BaseEntity where TSubclass : BaseEntity
    {
        public abstract object ID { get; }
        public virtual Expression> UpdateCriterion()
        {
            return entity => entity.ID;
        }
    }
    
    public partial class Foo : BaseEntity
    {
        public Int32 FooId { get; set; }
        public override object ID { get { return FooId; } } 
    }
    

    Just a thought - I only tried compiling in LinqPad and checking the value of a call to UpdateCriterion. :)

提交回复
热议问题