C# 将实体框架属性映射到数据库中的XML
我有一门课是这样的:C# 将实体框架属性映射到数据库中的XML,c#,xml,asp.net-mvc,entity-framework,C#,Xml,Asp.net Mvc,Entity Framework,我有一门课是这样的: public class GeneralClass { public int Id {get; set;} public string Name {get; set;} } public class DerivedClass: GeneralClass { public int SpecificProperty {get; set;} public string AnotherSpecificProperty {get; set;}
public class GeneralClass
{
public int Id {get; set;}
public string Name {get; set;}
}
public class DerivedClass: GeneralClass
{
public int SpecificProperty {get; set;}
public string AnotherSpecificProperty {get; set;}
public bool BooleanSpecificProperty {get;set;}
}
CREATE TABLE
General(Id int primary key,
Name nvarchar(50),
SpecificProperties xml);
这是一个非常通用的类。我还有这样一个派生类:
public class GeneralClass
{
public int Id {get; set;}
public string Name {get; set;}
}
public class DerivedClass: GeneralClass
{
public int SpecificProperty {get; set;}
public string AnotherSpecificProperty {get; set;}
public bool BooleanSpecificProperty {get;set;}
}
CREATE TABLE
General(Id int primary key,
Name nvarchar(50),
SpecificProperties xml);
问题是,我的应用程序中可以有很多派生类(超过10个),这就是为什么数据库中的继承不是一个选项。
我提出的解决方案是将GeneralClass
作为一个表,用一个包含特定属性的XML列
大概是这样的:
public class GeneralClass
{
public int Id {get; set;}
public string Name {get; set;}
}
public class DerivedClass: GeneralClass
{
public int SpecificProperty {get; set;}
public string AnotherSpecificProperty {get; set;}
public bool BooleanSpecificProperty {get;set;}
}
CREATE TABLE
General(Id int primary key,
Name nvarchar(50),
SpecificProperties xml);
其中,特定属性包含派生类的属性
问题是:如何使用实体框架保存和查询此xml列,并将xml反序列化为属性?
提前感谢 “…我的应用程序中可以有很多派生类(超过10个),这就是为什么数据库中的继承不是一个选项”-为什么不是强>
有两种主要的继承模式——每个层次的表(TPH)和每个类型的表(TPT)。虽然第一个可能看起来相当浪费SQL资源,但第二个可能符合您的需求。每种混凝土类型(TPC)还有一个模式-表,可将其视为TPT的变体。您的层次结构中没有抽象类,或者至少您发布的代码段没有抽象类,因此使用抽象类是很自然的
使用您非常熟悉的方式进行TPT和查询,比如LINQ to entities。有关EF和继承模式的更多信息以及
不过,如果您坚持使用XML,您可以将字段声明为XML类型(这是一种列数据类型,SQL server支持)。您可以像前面描述的那样进行查询。据我所知,EF不支持XML查询。所以我猜你必须在select之后应用一个谓词。