简体   繁体   English

可以在LINQ到SQL映射中使用泛型吗?

[英]Is possible to use generics in LINQ-to-SQL mapping?

Is there a way to define the following structure in a DataContext/DBML file? 有没有一种方法可以在DataContext / DBML文件中定义以下结构?

public class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public EntitySet<IPermission> Permissions { get; set; }
}

public class User : IPermissionHolder
{
    public int Id { get; set; }
    public string Name { get; set; }
    public EntitySet<Permission<User>> Permissions { get; set; }
}

public class Group : IPermissionHolder
{
    public int Id { get; set; }
    public string Name { get; set; }
    public EntitySet<Permission<Group>> Permissions { get; set; }
}

public interface IPermissionHolder
{
    int Id { get; set; }
}

public interface IPermission
{
    Entity Entity { get; set; }
    IPermissionHolder Holder { get; }
}

public class Permission<T> : IPermission where T : class, IPermissionHolder 
{
    public IPermissionHolder Holder
    {
        get { return PermissionHolder; }
    }

    public T PermissionHolder { get; set; }
    public Entity Entity { get; set; }
}

If it's not possible, can you seggest another structure that fits my need? 如果不可能,您是否可以隔离另一个适合我需要的结构?

Right now my DB is using two different tables for the GroupPermissions and the UserPermissions. 现在,我的数据库正在为GroupPermissions和UserPermissions使用两个不同的表。
I don't like to have a common table where i have to add a "type" column... with two different table i have a much more strict control on the DB side. 我不喜欢有一个公用表,在该表中我必须添加一个“类型”列...使用两个不同的表,我对数据库方面的控制要严格得多。

Thanks for any help 谢谢你的帮助

PS: i'm still with the Framework 3.5, otherwise i could remove the IPermissionHolder interface and use co-variance PS:我仍然使用Framework 3.5,否则我可以删除IPermissionHolder接口并使用协方差

PSS: asked also here, but no answer :( http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/04a03c68-79c0-4136-907c-f81440e78c45 PSS:也在这里问,但没有答案:( http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/04a03c68-79c0-4136-907c-f81440e78c45

EDIT: i'm trying different things and i'm facing two main problems 1) I want to have a IEnumerable, but it will never works because i don't want only to get data, but also to push data and an object can not be covariant and contravariant at the same time. 编辑:我正在尝试不同的事情,并且面临两个主要问题1)我想拥有一个IEnumerable,但是它将永远无法正常工作,因为我不仅要获取数据,而且还要推送数据和对象可以不能同时协变和相反。 So first of all i should choose: read or write. 因此,我首先应该选择:读取或写入。 2)Here the most difficult issue: how do i map TWO Association to a single property? 2)最困难的问题是:如何将两个协会映射到单个物业?
User: 用户:

[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "User_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "UserId")]
        public EntitySet<Permission<User>> Permissions{ ... }

Group

[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Group_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "GroupId")]
public EntitySet<Permission<Group>> Permissions { ... }

Permission 允许

   [global::System.Data.Linq.Mapping.AssociationAttribute(Name = "???", Storage = "holder", ThisKey = "HolderId", OtherKey = "Id", IsForeignKey = true)]
        public T PermissionHolder { ... }

Maybe i should call the Asscoiation "Holder_Permission"?!? 也许我应该将Asscoiation称为“ Holder_Permission” ?!

I tried with many different approach. 我尝试了许多不同的方法。 I can say that with LINQ-TO-SQL is not possible to have generic mapping. 我可以说使用LINQ-TO-SQL不可能具有通用映射。

I will try with the Linq-To-Entity. 我将尝试使用Linq-To-Entity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM