In EF Core how do you declare the name of the table it will create. Currently when you use "Add-Migration" it uses the name of the model when it create the table. Is there anyway to change that instead of using the name of my model.
Simplest way is to add a TableAttrbiute to your entity classes. Eg
[Table(Name = "customer_orders")]
public class CustomerOrder
{
// ...
}
This can also be done in DbContext.OnModelCreating using the Fluent API.
Related
I'm wondering if there is any way to map a stored procedure result into into a class.
This is what I have:
// DbContext Class:
public DbSet<StoredProcedureModelResult> SPMR { get; set; }
// Service Class:
var result = ctx.Set<StoredProcedureModelResult>().FromSql("getXXXX p1 = {0},1).ToList();
This works well, however, I'm using database migrations and every time I add one this thing creates StoredProcedureModelResult as a table...
I tried to ignore it onModelCreating:
modelBuilder.Ignore<SPModels.test>();
but this throws an exception:
Cannot create a DbSet for 'StoredProcedureModelResult ' because this type is not included in the model for the context
I just want use the benefit of mapping, and not to get a table creation, this is a simple model (non-entityType)
BTW
.NET Core 1.1
Web API
EF Core (code-first)
Thank you
You don't need to make the type an Entity type (by registering a DbSet in your DbContext). Just have a method that returns IEnumerable<StoredProcResults> (can be in your DbContext), and in it run
dbContext.Database.SqlQuery<StoredProcResults>(...)
See Database.SqlQuery<T>(...)
public DbSet<Lecture> Lectures{ get; set; }
Does the property name here matter at all? It seems that if I want to use the model, I use "Lecture". The generated table is just a plural of whatever is in <>, e.g., if I understand correctly, I can change "Lectures" to "Leprechauns" and my table will still be called "Lectures" based on <Lecture> and I will use context.Lectures to select from it. Does the property name have any point?
I didn't find the answers in this tutorial or on msdn.
Edit: Upon further testing - the db table name is based on the model name in the angle brackets, but to actually select from the db (in the C# code), you use the property name specified in DbSet propertyName. Still would like to hear how this works in detail.
Entity Framework builds a model of the database, where each class/model represents an entity type, and each DbSet represents a set of entities of a single type. When you declare a DbSet<T> property in your DbContext, that tells EF to include the class of type T as one of the entity types, and it automatically includes any other connected types (e.g. navigation properties) in the object graph as well.
All this to say, the name of the property itself probably doesn't matter. In fact, you could use the Fluent API to add entity types as well, not declare any DbSet properties if you wanted, in which case you'd use context.Set<T> to retrieve the DbSets. The properties are really just for convenience.
Maybe this is helpful as well: https://msdn.microsoft.com/en-us/data/jj592675.aspx
DbSet corresponds to a table or view in your database, So you will be using your DbSet's to get access, create, update, delete and modify your table data.
By the way you can remove the convention:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
The property name matters. The EF translates the name of the property into the name of the table. If the property name is not the same with the table name you'll get an error. Unless you specifically tell the builder the name of the table like this:
public void Configure(EntityTypeBuilder<Lecture> builder)
{
builder.ToTable("License");
}
I am using EF 4.3 and am adding Audit fields to my classes and tables. I have a service layer which is getting the credentials of the client applications by using the OperationContext, so I am passing that information to my tables through EF mappings. An example of this would be:
class A
{
string CreatedByUser { get; set; }
}
class B : A
{
}
I am using the fluent interface to provide my POCO to table mappings -- when I map the CreatedByUser column in the base and derived class, the derived class mappings do not take effect and the information is not passed to the database.
I have gotten around this by creating fields in my base class for the derived classes to use that are just pass-throughs of the audit columns but this is messy.
Try making class A abstract, I think that will give you the effect you are looking for(add the columns from class A to all class/tables that inherit it)
Building a C# application using EF 4.1 with Database First. My question centers around using extension methods to return values through the DbContext object.
My entities were created as partial classes by the DbContext generator. I have a Projects table and a related Managers table and through the context object I can perform LINQ queries and access projects and the assigned project manager. If the Manager table has firstname and lastname fields how would I write my extension as a property or method to concatenate the first and last name fields so that when I query for a project and use the manager relation the property FullName returns the combined value?
This example has a variable _projects populated with a list of Projects created using LINQ query:
Fields["manager"].Value = _projects[i].Project.ProjectManager.FullName
I can makethe FullName property into a method and pass the id for the manager however wouldn't that make it into an additional query? Thanks for the help.
If you know that you're always going to be accessing the ProjectManager navigation property, you can include it in the initial query by using the Include method
var projects = (from p in ctx.Projects
select p)
.Include(p => p.ProjectManager)
.ToList();
Then you can add the following property to your Manager class (in a seperate file than the generated one)
public string FullName
{
get { return FirstName + " " + LastName; }
}
And access it using
var manager = projects[0].ProjectManager.FullName;
If I missinterpreted your question, could you provide an example of what your code would look like using your proposed extension method?
i want extend an entity framework model with a temporany attribute.
I need it only in a mvc form. I don't need save it in the db.
How can i do it?
Create a partial class for the entity you want to extend
e.g.
//must be in the same namespace as the Customer entity in the model
public partial class Customer
{
public string MyProperty{get;set;}
}
This property will be unmapped and you can fill it with data after you run a query or on materialization.
OR
Create a wrapper class for your entity which expose both the unmapped property and the mapped properties the properties you need in the view.