Let's say you want to say a entity needs to use a view for querying the data and use the table to insert, update or delete the data. In general this is not possible.
Indirectly you can set the table of an entity to the view and use stored procedures for commanding purposes.
For mapping of an entity you inherit EntityTypeConfiguration object to configure you mapping definition. In this class you can define the mapping to the table and also define the stored procedures for insert, update and delete.
For Example:
class PlannableEmployeeConfiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration
{
public PlannableEmployeeConfiguration()
{
ToTable("ResourceEmployee"); //view
HasKey(t => t.Id);
MapToStoredProcedures(m =>
{
m.Delete(delConfig =>
{
delConfig.HasName("PlannableEmployeeDelete");
delConfig.Parameter(pe => pe.Id, "Id");
});
m.Insert(
insertConfig => { insertConfig.HasName("PlannableEmployeeInsert"); }
);
m.Update(
updateConfig => { updateConfig.HasName("PlannableEmployeeUpdate"); }
);
}
);
}
}
Now you can add the configuration to dbBuilder.
dbBuilder.Configurations.Add(new PlannableEmployeeConfiguration());
And generate the stored procedures via the console with "add-migration". In the generated migration file you need to change the stored procedures; Why?
Because in the generated stored procedures the view is used to insert, update or delete the data. This is because you used the view to map with ToTable. Change the sql to use the table and you are good to go to update the database
Hope this will help you for having a seperate query and command using the EF code first principles.
Geen opmerkingen:
Een reactie posten