可以一个LINQ to SQL的IsDiscriminator柱不能承受? [英] Can a LINQ to SQL IsDiscriminator column NOT inherit?

查看:131
本文介绍了可以一个LINQ to SQL的IsDiscriminator柱不能承受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计我的数据库和LINQ to SQL ASP.NET Web应用程序。

I'm designing my database and LINQ To SQL ASP.NET web application.

想象一下,我有两种类型的网页:正常和根。有些是根。有些则不是。

Imagine I have two types of pages: normal and root. Some pages are roots. Some pages are not.

我有一个网页,数据库表和RootPage数据库表:

I have a Page database table and a RootPage database table:

      Page
      ----
   PK PageId
      HtmlTitle
      PageHeading
      MetaDescription
      IsRoot

      RootPage
      --------
FK PK PageId
      FavIcon
      StyleSheet
      MasterPage

我想,如果在我的DBML文件,我设置了IsRoot列的IsDiscriminator属性,那么我RootPage类将继承Page类。

I think that if within my DBML file I set the IsDiscriminator property of the IsRoot column, then my RootPage class will inherit the Page class.

我希望能够以这样的工作在我的code:

I want to be able to work like this in my code:

MyDataContext db = new MyDataContext();

var roots = from p in db.Pages
            where p is RootPage
            select (RootPage)p;

或者是这样的:

Or like this:

RootPage r = new RootPage();
r.HtmlTitle = "Foo";
r.FavIcon = "bar.ico";
...
db.Pages.Add(r);
db.SubmitChanges();

能否LINQ to SQL的IsDiscriminator列可为空还是假的?将这项工作?

Can a LINQ to SQL IsDiscriminator column be nullable or false? Will this work?

推荐答案

这里的问题是,你正在试图分裂两个表,RootPage和页面之间的类。

The problem here is that you are trying to split your class between two tables, RootPage and Page.

不幸的LINQ to SQL只支持单表的传承所以这是行不通的。

Unfortunately LINQ to SQL only supports single table inheritence so this would not work.

您将需要合并两个表的定义放在一起,使RootPage特有的字段为空。例如,

You would need to merge the two table definitions together and make the RootPage-specific fields nullable. e.g.

   Page
   ----
PK PageId
   HtmlTitle
   PageHeading
   MetaDescription
   IsRoot
   FavIcon (Nullable)
   StyleSheet (Nullable)
   MasterPage (Nullable)

您会接盘IsRoot是鉴别和Page类作为默认类型,RootPage标记为作为类的鉴别值'真'。

You would then set IsRoot to be the discriminator and mark the Page class as the default type and RootPage as being the class for the discriminator value of 'True'.

这是选择,如果你不介意的事情都是只读将是使一种观点认为,加入了两个表一起和基类的断

An alternative if you didn't mind things being read only would be to make a view that joined the two tables together and base the classes off that.

第三种选择可能是考虑组成,如重命名RootPage表根和创建RootPage与根之间的关联。这意味着,而不是你RootPage类具有所有这些属性将替代只会暴露他们实际居住的根属性。

A third option might be to consider composition such as renaming the RootPage table to Root and creating an association between RootPage and Root. This would mean that instead of your RootPage class having all those properties it would instead only expose the Root property where they actually reside.

这篇关于可以一个LINQ to SQL的IsDiscriminator柱不能承受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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