2

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.

2
  • how many entities do you have with the "same" name properties? the reason I am asking, because unless it is very large number the complexity you are introducing is not worth the benefit Commented Mar 17, 2011 at 16:29
  • There are not so many entities in one particular project, though such mechanism will save me plenty of time, as I will encounter this issue in many future projects. Commented Mar 17, 2011 at 17:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Dear Jim, thank you for your reply, I'll consider to use EF in the future.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.