Friday, March 25, 2016

How to add field not mapped to table in Linq to Sql

Leave a Comment

In Entity Framework I can apply NotMapped attribute to a property which I do NOT want to create a column in a database table for. How to get the same effect for auto generated classes in DBML file? I have a StoredProcedure that returns some additional fields. I called SP like:

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name = "dbo.sp_GetSupplierArticles")]     public ISingleResult<SupplierArticle> GetSupplierArticles(         [global::System.Data.Linq.Mapping.ParameterAttribute(DbType = "BigInt")]         long mainArticleId,         [global::System.Data.Linq.Mapping.ParameterAttribute(DbType = "BigInt")]         long? userId         )     {         IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), mainArticleId, userId);         return ((ISingleResult<SupplierArticle>)(result.ReturnValue));     } 

Necessary field I added into the separated partial class. Without any additional attributes it returns default value for my and applied [Column(IsDbGenerated = false)] in the separated partial class:

   public partial class SupplierArticle    {         [Column(IsDbGenerated = false)]         public double Extra { get; set; }    } 

So it works until I try to get SupplierArticle using another query (not my stored procedure):

db.LoadOptions = db.GenerateDataLoadOptions(entitiesToInclude); var query =     from shoppingCartItem in db.ShoppingCartItems     where shoppingCartItem.UserId == userId     select shoppingCartItem; return query.ToList(); 

My entity is loaded due to LoadOptions (passed in entitiesToInclude parameter). In this query and another which try to load "poor" entity with properties that defined in .dbml file I get exception: Invalid column name 'Extra' and the same message for each additional property.

What is the proper way to extend entity or how to avoid that exception?

UPD: If I remove all attributes exception no longer occurs. But added properties are not initialized when SP returns a result.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment