与实体框架向下转

与实体框架向下转

本文介绍了与实体框架向下转型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我在EF一个雇主作为一个派生类用户的已经定义了一个项目。在我的过程我不知道它是否会最终成为雇主(或其他类型的用户)创建一个用户,后来我需要将其转换。起初我试过(智能感知表明一个明确的转换存在):

I have a project where I've defined in EF an Employer as a derived class of User. In my process I create a user without knowing whether it will eventually be an employer (or other kinds of users) and later I need to convert it. At first I tried (Intellisense indicated an explicit conversion exists):

Employer e = (Employer) GetUser();

但在运行时我得到了:

but at runtime I got:

Unable to cast object of type 'System.Data.Entity.DynamicProxies.User_7B...0D' to type 'Employer'.

所以我试着写一个转换器:

so I tried to write a converter:

public partial class User
{
    public static explicit operator Employer(User u)
    {

但我得到的错误:

but I get the error:

Error   21  'User.explicit operator Employer(User)': user-defined
conversions to or from a derived class are not allowed
C:\Users\..\Documents\Visual Studio 2010\Projects\..\Website\Models\EF.Custom.cs

罚款。然后我重载雇主构造是这样的:

public partial class Employer
{
    public Employer(User u)
    {
        this.Id = u.Id;
        this.Claims = u.Claims;
        // etc.
    }
}

和我能够再只是做:

Employer e = new Employer(GetUser());

但是当我运行它,我得到的错误:

but when I run it I get the error:

System.InvalidOperationException was unhandled by user code
  Message=Conflicting changes to the role 'User' of the
  relationship 'EF.ClaimUser' have been detected.
  Source=System.Data.Entity
  StackTrace:
       [...]
       at Controllers.AuthController.Register(String Company, String GivenName,
       String Surname, String Title, String Department) in C:\Users\..\Documents\
       Visual Studio 2010\Projects\..\Website\Controllers\AuthController.cs:line

作为最后的手段我试着写这样的:

as a last resort I tried writing this:

        Employer e = Auth.Claims("id")
            .Where(x => x.Value == Auth.NameIdentifier())
            .Select(x => x.User)
            .Cast<Employer>()
            .Single();

...的getUser()返回类型的对象用户不提供 .Cast&LT;&GT; 所以我用直接查询到那里......但是我还是动态代理的投射对象除外。

... GetUser() returns an object of type User which does not offer the .Cast<> so I used a direct query to get there... but I still get the casting of dynamic proxy objects exception.

所以我的问题是:我怎么能向下转换当对象通过EF具有持久性。

so my question is: how can I downcast when the object has persistence through EF?

推荐答案

这是不可能的。你必须始终使用最后一种类型。一旦你创建一个用户,EF绝不允许你将其更改为派生实体类型。

It is not possible. You must always use final type. Once you create it as a User, EF will never allow you changing it to a derived entity type.

顺便说一句。它也不可能用面向对象的方法为好。你不能投父类的实例派生类的实例(除非它真的是派生类的实例) - 它会在运行时抛出异常。很简单的例子来重现问题:

Btw. it is also not possible with object oriented approach as well. You cannot cast instance of parent class to the instance of derived class (unless it really is instance of derived class) - it will throw exception at runtime. Very simple example to reproduce the issue:

class X { }

class Y : X { }

class Program
{
    static void Main(string[] args)
    {
        X x1 = new Y();
        Y y1 = (Y)x1;   // Works

        X x2 = new X();
        Y y2 = (Y)x2;   // InvalidCastException
    }
}

做到这一点是压倒转换运算符将在内部创建派生类的新实例,并从旧的父实例复制所有领域的新的导出的一个唯一的办法。

The only way to do that is overriding conversion operator which will internally create a new instance of derived class and copy all fields from the old parent instance to that new derived one.

需要与实体框架完全一样的做法。如果您开始使用用户实体,现在要将其推广到雇主实体必须删除旧的用户,并创造新雇主

Exactly the same approach is needed with entity framework. If you started with User entity and now you want to promote it to Employer entity you must delete old user and create new Employer.

这篇关于与实体框架向下转型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:16