EF Codefirst将基类转换为派生类 [英] EF Codefirst Convert Base Class to Derived Class

查看:160
本文介绍了EF Codefirst将基类转换为派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下实体:

  public class Kisi 
{
[Key]
public int KisiID {get;组; }
public string Ad {get;组; }
public string Soyad {get;组; }

public virtual ICollection&Grill> Gruplar {get;组; }
public virtual ICollection< Kampanya> Kampanyalar {get;组; }
}

public class Musteri:Kisi
{
public int? Yas {get;组; }
public string Meslek {get;组; }

}

这两个类在SQL中存储一个表(TPH)服务器。



我保存了一个Kisi,这可能与其他表相关。
我怎样才能将其转换/转换/推广到Musteri,保持相同的ID?我不能重新创建。



我可以发出一个手动SQL INSERT,但它是一种丑陋的...



如何处理它,而不会丢失KisiID?

解决方案

这不可能没有绕过EF的抽象。 EF不允许您在运行时更改实体类型。您可以使用SQL Update语句手动更新相应的行。


Supposing the following entities :

public class Kisi
{
    [Key]
    public int KisiID { get; set; }
    public string Ad { get; set; }
    public string Soyad { get; set; }

    public virtual ICollection<Grup> Gruplar { get; set; }
    public virtual ICollection<Kampanya> Kampanyalar { get; set; }
}

public class Musteri : Kisi
{
    public int? Yas { get; set; }
    public string Meslek { get; set; }

}

These two classes storing one table(TPH) in SQL SERVER.

I saved a Kisi and this could be in relation to other tables. How can I cast/convert/"promote" it to a Musteri, keeping the same ID ? I can't recreate.

I could issue a "manual" SQL INSERT, but it's kind of ugly...

How can i handle it without loosing the KisiID ?

解决方案

This is not possible without bypassing the abstraction of EF. EF does not allow you to change the entity type at runtime. The discriminator column is not exposed by EF.

What you can do is manually update the corresponding row using a SQL Update statement.

这篇关于EF Codefirst将基类转换为派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆