简体   繁体   English

使用EntityFramework将我的实体变量转换为特定的(实体)类型

[英]Casting my entity variable to a specific (entity) type with EntityFramework

I have some classes like this one below: 我下面有一些这样的课程:

public class Driver
{
    public int       Id { get; set; }
    public string    Firstname { get; set; }
    public string    Lastname { get; set; }
    public string    CreatedById { get; set; }
    public DateTime? CreatedTime { get; set; }
    public string    UpdatedById { get; set; }
    public DateTime? UpdatedTime { get; set; }
}

Please note the 4 last fields. 请注意最后4个字段。

Everytime an entity is created/updated, I call a function to update 4 fields which are present in all my classes (CreatedTime, CreatedById, UpdatedTime, UpdatedById). 每次创建/更新实体时,我都会调用一个函数来更新我所有类(CreatedTime,CreatedById,UpdatedTime,UpdatedById)中存在的4个字段。

The example below is for the Driver class: 下面的示例适用于Driver类:

    private void UpdateAdditionalData(EntityInfo entityInfo)
    {
        var userId = Current.User.Identity.Name;
        var entity = (Driver)entityInfo.Entity;
        ... 
        if (entityInfo.EntityState == EntityState.Added)
        {
            entity.CreatedTime = DateTime.Now;
            entity.CreatedById = userId;
        }
        if (entityInfo.EntityState == EntityState.Modified)
        {
            entity.UpdatedTime = DateTime.Now;
            entity.UpdatedById = userId;
        }

As you can see, I declare an entity variable which is casted with a prefix (Driver) . 如您所见,我声明了一个以前缀(Driver)强制转换的entity变量。

Now I would like to adjust this code to be able to reference any classes and not specifically the Driver class. 现在,我想调整此代码,使其能够引用任何类,而不是专门引用Driver类。

I tried something like: 我尝试了类似的东西:

    private void UpdateAdditionalData(EntityInfo entityInfo)
    {
        var userId = Current.User.Identity.Name.ToUpper();   
        var typed = entityInfo.Entity.GetType().Name;
        var entity = (typed)entityInfo.Entity;
        ...

So I declared a typed variable which is the name of the entity for casting my entity. 因此,我声明了一个typed变量,它是用于转换实体的实体名称。

But it doesn't work, I got an error: the type of namespace 'typed' could not be found. 但这不起作用,我得到一个错误:找不到“类型化”名称空间的类型。

Any idea how can I accomplish this? 知道我该怎么做吗?

Thanks. 谢谢。

Bottom line of why this doesn't work is normally types are static, compile time data. 通常,此类型无效的底线是静态的编译时数据。 You're trying to get it at runtime. 您正在尝试在运行时获取它。 This means using reflection and invocations which will be complicated. 这意味着将使用复杂的反射和调用。

I think what you want is an interface to work with that's shared across entities. 我认为您想要的是一个可在实体之间共享的接口。

public interface Auditable
{
    string    CreatedById { get; set; }
    DateTime? CreatedTime { get; set; }
    string    UpdatedById { get; set; }
    DateTime? UpdatedTime { get; set; }
}

Then have your entity implement that interface. 然后让您的实体实现该接口。 Any entity that implements that interface can be used... 可以使用实现该接口的任何实体...

var userId = Current.User.Identity.Name.ToUpper();   
var entity = entityInfo.Entity as Auditable;    
if (entity != null) { /* set the audit values */ }

If the entity doesn't implement the interface then you won't actually set the audit values. 如果该实体未实现该接口,则您实际上将不会设置审核值。

Unless I misunderstand you, you need a base type for your entities? 除非我对您有误解,否则您是否需要实体的基本类型?

public class Driver : EntityBase
{
    public int       Id { get; set; }
    public string    Firstname { get; set; }
    public string    Lastname { get; set; }
}

Base type... 基本类型...

public class EntityBase
{
    public string    CreatedById { get; set; }
    public DateTime? CreatedTime { get; set; }
    public string    UpdatedById { get; set; }
    public DateTime? UpdatedTime { get; set; }
}

Then you could simply cast to your base type? 那么您可以简单地将其强制转换为基本类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM