开发者

Linq to Sql inheritance mapping to multiple tables

I am having trouble with inheritance mapping in Linq to Sql. I am using MSDN as a reference and as a basis it sounds good. However the example it gives is a single table inheritance mapping. However, I am trying to do multiple table inheritance to save on table space. Is this possible? So far I have:

[Table(Name="writing_objs")]
[InheritanceMapping(Code="T",Type=typeof(ObjectTypeA), IsDefault开发者_运维技巧=true)] // Only default because a default is required
[InheritanceMapping(Code="N",Type=typeof(ObjectTypeb))]
public abstract class WritingObject
{
    /* ... */

    [Column(Name="obj_tp", IsDiscriminator=true)]
    [StringLength(1)]
    public string ObjectType { get; set; }
}

I then have the different object types defined like so:

[Table(Name="obj_type_a")]
public class ObjectTypeA: WritingObject
{
    /* Object Type A fields */
}

The issue seems to be that I am defining a table attribute in the 2nd type, as I get the following exception:

The inheritance subtype 'ObjectTypeA' is also declared as a root type.

Is it possible to keep these fields in separate tables with Linq to Sql or am I going to have to consolidate them all into a single table? Is it necessarily bad to have some extra fields in one table as long as there aren't too many (some object types might even be able to share some fields)?


Linq to SQL does not support multiple-table inheritance using a discriminator, even though that is the best design in many cases (it's the most normalized).

You'll have to implement it using associations instead. If you use a mapping layer that converts it to an inheritance-based domain model, it will be easier to manage at higher layers.


Well I know this problem has already been resolved, but as I just encountered the same issue, I'd like to share what I did :

Just remove the [Table] attribute from your inherited classes. And it's quite logical, because we define in the generic classes a way to store all subtypes (with the discriminatory attrbute).

Maybe this will help someone in the future.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜