开发者

Linq to sql, inherit repeated column

everyone. I've got an issue with linq to sql.

I'm using attributes from System.Data.Linq.Mapping to map my classes and tables. I've noticed, that some column definitions are repeated in my code (like ID's, for example). So I'd like to move such definitions to the base class. And I've wrote something like this:

public abstract class BasicEntity
{
    [Column(IsDbGenerated=true, IsPrimaryKey=true)]
    public int ID { get; set; }
}

[Table(Name = "Users")]
public class User : BasicEntity
{
    [Column(Name = "Name")]
    public string Name { get; set; }
}

public class TestDataContext : DataContext
{
    public TestDataContext(string connectionString) : base (connectionString) { }
    public Table<User> Users { get { return this.GetTable<User>(); } }
}

....

{
    TestDataContext context = new TestDataContext(validConnectionString);
    if(context.DatabaseExists())
        context.DeleteDatabase();
    context.CreateDatabase(); // an exception thrown from here
}

So, when I try to create a database, I've got an开发者_如何学编程 System.Data.SqlClient.SqlException, that says: "Column named "ID" is not present in the targeted table or view." Well, obviously, this exception is caused by my BasicEntity.

Finally, my question is: Is there a way to inherit column definitions to remove repeated code?

And to be clear - I use #develop, which does not have fancy LINQ2SQL designer/codegenerator, like VS.

Many thanks in advance.


In LINQ to SQL, the base class and derived class properties must exist in the same table. In your case, I suspect that the common fields exist in multiple tables. LINQ to SQL does not support this kind of mapping. You will likely need to use EF for this kind of inheritance mapping.


I ran into a similar situation in that I was working with a database with 8 different tables with a primary key and 8 foreign key constraints.

All 8 tables shared the exact same column names and foreign key contraints with only their data fields and their primary key fields having different column names/types etc.

I wanted to make an SQL Linq DataContext with entities for it, but I didn't want to have to define the same 8 columns on each type. So I put them in the base type XYZDataEntryEntity.

I ran into this error so I had an idea, I made all the Column's in the base class virtual. I tagged them with Column attributes in the base class, and I simply overrode them in the derrived classes, like

    public override int InstitutionID
    {
        get
        {
            return base.InstitutionID;
        }
        set
        {
            base.InstitutionID = value;
        }
    }

Then I got complaints because the Private field was not available, e.g.

[Column(Storage = "_XYZStatus", DbType = "Int NOT NULL=", Name = "Column_Name_here")]

_XYZStatus was not available, and I thought, yeah because it's in the base class.

So I made _XYZStatus protected instead of private so the derrived class could see it, and like magic. Everything worked.

Sure I have to override the same 8 properties in every class for it to work, but I only have to make changes to them in the base class, and it at least binds them all to the same base class and the same 8 properties.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜