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.
The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database. In the following example, the AuditLog class will not be mapped to a table in the database: public class Contact. {
Just drop the view on the DBML designer surface. Show activity on this post. If you have defined the views in you SQL Server Database, they'll appear in the Server Explorer and can be dragged and dropped onto the DBML diagram. The view(s) will not have any relation to other tables.
LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.
In SQL Server, a SQL statement like 'NULL=NULL' evaluates to false. however 'NULL IS NULL' evaluates to true. So, for NULL values in your database columns, you need to use the 'IS' operator instead of the regular '=' operator.
I would suggest creating a complex type for that stored procedure. I would even go as far as creating complex types for all of your stored procedures as this is best practice. You can then add an extension method, or a method to your partial classes that will convert the complex type returned from the stored procedure to it's related entity, and vice versa. Another option would be to include a foreign key to your complex stored procedure type, and a navigation property pointing to the correct entity.
These are, of course, solutions to a problem that EF itself doesn't address. This is expected as EF is an ORM and is not concerned with what's not persisted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With