I'm using EF5 to produce a model from an existing DB structure. I map Insert/Update/Delete Stored Procedures to the entities. This is working fine.
What I would like to do next is pass a UserId to these SPs as a parameter but not have the UserId as a column within the underlying table (the SPs will utilize this parameter). I have lots of entities. Is it possible to somehow add a property that will always be added back in even after updating the model from the DB?
Many Thanks
If you are using EDMX to generate the Entity Framework object model, your classes are partial and you can add properties to the partial classes of your entities which will survive database regeneration.
If you have a ParticularEntity table in the DB and referenced in the EDMX, you may add a partial class file ParticularEntity.Augments.cs (the name is for your reference, and you can have multiples as normal with partial classes) to your project and within it
public partial class ParticularEntity
{
public string UserId { get; set; }
public void DoSomething(string userId)
{
someFunctionThatYouWantToNotBeAnExtension();
}
}
Alternatively, you could write a set of extension methods which your maps utilize. I don't think that's as clean as extending the EF classes with partials, though.
Entity created by EF are partial class so you can extend that class with your custom properties
YourEntity.cs //created by EF
public partial class YourEntity
{
public string Name { get; set; }
...
}
YourEntityExtended.cs // created by you
public partial class YourEntity
{
public int Id { get; set; }
}
Related
I have a ASP.NET MVC + EF web application, but I get an error when trying to query a list of Product using LINQ:
Invalid column name 'Discriminator'. Invalid column name 'NewProductPhoto'
This happened after I added a derived class from one of my entities.
My entity class:
[Table("Product")]
public partial class Product
{
public Product()
{
ProductPhotos = new HashSet<ProductPhoto>();
SalesOrderItems = new HashSet<SalesOrderItem>();
}
// more attributes here
}
My new derived class:
public class ProductEx : Product
{
public byte[] NewProductPhoto { get; set; }
}
I understand that EF tries to distinguish between Product and ProductEx but the point is I have no intention to add ProductEx to EF model.
I don't know why it considers ProductEx as part of database model.
How can I tell EF to not consider ProductEx as database model.
I found that the same way I can exclude a property from entity framework persistent, I can exclude my ProductEx class from entity framework modeling.
The attribute [NotMapped] does the trick.
[NotMapped]
public class ProductEx : Product
{
public byte[] NewProductPhoto { get; set; }
}
I still don't understand why EF even considers my derived class for persistence.
I have (can`t change) EF DataBase first project without navigation property in models.
I want extend autogenerated models and add navigation property
Generated model
//generated.cs
public partial class company
{
public int id { get; set; }
public string name { get; set; }
}
public partial class user
{
public int id { get; set; }
public int company_id { get; set; }
}
I want add navigation property from code
//model_extension.cs
public partial class user
{
public company Company { get; set; }
}
I have exception "The specified type member 'Company' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
I work with CodeFirst before.
I understand, I must link user.company_id to Company
But not understand how make this with code (not designer)
In Database First Approach, You are generating your POCO objects from database schema via Entity Framework Designer/ADO.NET Entity Data Model so it is not flexible as Code-First, you need to go on database, and change the schema yourself and update your .edmx file. while adding properties to these Models are possible in c# side, but they are not going to be added to your database schema,
I suggest your reverse your database schema and go as Code-First Approach, This nuget package can do this for you.
After Reversing It's all about Code-First then, creating your own DbContext and OnModelCreating and let the Migration handle the rest. Then you can use Eager Loading of EF to load your data,
I am using Entity Framework Database First for working with db.
I have a base class EntityBase
public class EntityBase
{
public Int32 Id { get; set; }
}
I have some other classes that are generated by EnitytFramework and represents tables of my db, for example, "User" class:
public class User
{
public int32 Id{get;set;}
public class Name{get;set;}
public class Age{get;set;}
}
I want all EF model classes to be inherited from EntityBase:
public class User : EntityBase
I can do this manually, by deleting Id field from User class and defining inheritance, but after I am updating model from db, all manually-made changes dissapears.
Is there any way to keep or automatically add inheritance to EF model classes?
EF database first work with t4 files if you change manually the class, save and compile, T4 will generate the classes again and you lose your changes.
You must make this change in T4 template. Search the EntityClassOpening method and change the last line:
_typeMapper.GetTypeName(entity.BaseType)
with this code:
_typeMapper.GetTypeName(entity.BaseType) ?? "EntityBaseClass"
I have a DLL with a base model using code first EF 4.3.
What i want is to extend certain models in that dll with additional fields.
for example in BaseModel.DLL
namespace BaseModel
{
public class Account
{
public Id { get;set;}
public string Name {get;set;}
}
}
in a referencing project i want to extend the Account model (and DB table):
public class Account : BaseModel.Account
{
public string SomeAdditionalInfo { get;set;}
}
I want to end up with a table Account with fields
Id
Name
SomeAdditionalInfo
This way i can keep reusing the BaseModel (and logic) in several similar projects.
I guess i can't use partial classes because we're speaking different DLL's.
Maybe inheritance? I tried several ways but i keep getting conflicts about having 2 models with the same name.
Any hints? tips? solutions?
You can use inheritance though Table per Hierarchy.
You can create base class AccountBase and child class Account:AccountBase:
public class AccountBase
{
public Id { get;set;}
public string Name {get;set;}
}
public class Account : AccountBase
{
public string SomeAdditionalInfo { get;set;}
}
It generates Table AccountBase that will contains columns Id, Name, SomeAdditionalInfo
There will be also column Discriminator that will contains instance of what class contains in this row.
I have looked at two of Microsoft's tutorials for MVC. In one tutorial they are creating a .edmx file to handle the Entity Framework in order to execute Linq queries. In another tutorial they made a class called "MusicStoreEntities.cs" here is the code:
using System.Data.Entity;
namespace MvcMusicStore.Models
{
public class MusicStoreEntities : DbContext
{
public DbSet<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Cart> Carts { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
}
}
And the tutorial creates an instance of this class and starts doing Linq queries as well. What are the differences between these 2 methods? and how can I make DbSet objects in a .edmx file? Thank you.
There are various ways to create your model structure.
You can code POCO classes first, and create the database manually.
You can code POCO classes first, and create the database automatically using code first libraries.
You can create your database schema, and import it into a class diagram (which will supply all the models and navigation).
You can create your class diagram, and create your database schema from that.
1 and 2 create only the cs, while 3 and 4 create the edmx.
You can check this for Code First EF (this includes the DbSet part of your question).
EDIT: You can even use POCO classes with an existing database, as posted here.