Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Linq defines entity inheritance

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章给大家分享的是有关Linq如何定义实体继承的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Linq实体继承的定义

Linq to sql支持实体的单表继承,也就是基类和派生类都存储在一个表中。对于论坛来说,帖子有两种,一种是主题贴,一种是回复帖。那么,我们就先定义帖子基类:

[Table(Name = "Topics")]

public class Topic

{

[Column(Name = "TopicID", DbType = "int identity", IsPrimaryKey = true,

IsDbGenerated = true, CanBeNull = false)]

public int TopicID { get; set; }

[Column(Name = "TopicType", DbType = "tinyint", CanBeNull = false)]

public int TopicType { get; set; }

[Column(Name = "TopicTitle", DbType = "varchar(50)", CanBeNull = false)]

public string TopicTitle { get; set; }

[Column(Name = "TopicContent", DbType = "varchar(max)", CanBeNull = false)]

public string TopicContent { get; set; }

}

这些Linq实体继承的定义大家应该很熟悉了。下面,我们再来定义两个Linq实体继承帖子基类,分别是主题贴和回复贴:

public class NewTopic : Topic { public NewTopic() { base.TopicType = 0; } } public class Reply : Topic { public Reply() { base.TopicType = 1; } [Column(Name = "ParentTopic", DbType = "int", CanBeNull = false)] public int ParentTopic { get; set; } }

对于主题贴,在数据库中的TopicType就保存为0,而对于回复贴就保存为1。回复贴还有一个相关字段就是回复所属主题贴的TopicID。那么,我们怎么告知Linq to sql在TopicType为0的时候识别为NewTopic,而1则识别为Reply那?只需稍微修改一下前面的Topic实体定义:

[Table(Name = "Topics")]

[InheritanceMapping(Code = 0, Type = typeof(NewTopic), IsDefault = true)]

[InheritanceMapping(Code = 1, Type = typeof(Reply))]

public class Topic

{

[Column(Name = "TopicID", DbType = "int identity", IsPrimaryKey = true,

IsDbGenerated = true, CanBeNull = false)]

public int TopicID { get; set; }

[Column(Name = "TopicType", DbType = "tinyint", CanBeNull = false,

IsDiscriminator = true)]

public int TopicType { get; set; }

[Column(Name = "TopicTitle", DbType = "varchar(50)", CanBeNull = false)]

public string TopicTitle { get; set; }

[Column(Name = "TopicContent", DbType = "varchar(max)", CanBeNull = false)]

public string TopicContent { get; set; }

}

为类加了InheritanceMapping特性定义,0的时候类型就是NewTopic,1的时候就是Reply。并且为TopicType字段上的特性中加了IsDiscriminator = true,告知Linq to sql这个字段就是用于分类的字段。

感谢各位的阅读!关于"Linq如何定义实体继承"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report