entity-framework-6

What is the correct use of IDatabaseInitializer in EF?

邮差的信 提交于 2019-11-30 00:22:01
I have a custom DatabaseInitialiser which is below /// <summary> /// Implements the IDatabaseInitializer to provide a custom database initialisation for the context. /// </summary> /// <typeparam name="TContext">TContext is the DbContext</typeparam> public class ParikshaDataBaseInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext { /// <summary> /// The method to Initialise the database. /// Takes care of the database cannot be dropped since it is in use problem while dropping and recreating the database. /// </summary> /// <param name="context">The DbContext on

How can I use use Entity Framework to do a MERGE when I don't know if the record exists?

隐身守侯 提交于 2019-11-29 23:38:55
In this SO answer about Entity Framework and MERGE, the example for how to code it is this: public void SaveOrUpdate(MyEntity entity) { if (entity.Id == 0) { context.MyEntities.AddObject(entity); } else { context.MyEntities.Attach(entity); context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); } } This assumes that you already know if the entity that you want to upsert exists or not; in this case you check entity.Id . But what if you don't know if the item exists or not? For instance, in my case, I'm importing records from a vendor into my database, and a given record may

Separate POCO Object classes and DBContext from Entity Framework 6 Model

可紊 提交于 2019-11-29 23:11:16
I started to use Entity Framework 6.0.1 version. I want to separate the generated DbContext and POCO template classes to different class library from the model. I spent a few hours solve the problem without any success. If I create a new class library, add EF 6 EntityObject Generator and fill the following template variable: SourceCsdlPath = @"..\..\DataAccess\Model.edmx" , Get the following error in the error list after building: Error 2 Running transformation: System.IO.FileNotFoundException: Unable to locate file File name: 'C:\Source\EFsource\POCO....\DataAccess\SZOSZRDBModel.edmx' Server

EF 6 database first: How to update stored procedures?

让人想犯罪 __ 提交于 2019-11-29 22:49:40
We are using Entity Framework 6.0.0 and use database first (like this) to generate code from tables and stored procedures. This seems to work great, except that changes in stored procedures are not reflected when updating or refreshing the model. Adding a column to a table is reflected, but not adding a field to a stored procedure. It is interesting that if I go to the Model Browser , right click the stored procedure, select Add Function Import and click the button Get Column Information we can see the correct columns. This means that the model knows of the columns, but does not manage to

How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

霸气de小男生 提交于 2019-11-29 19:42:32
Assumptions Using EF 6.1, MVC 5, VS 2013, C# I have an existing database model designed in Toad DM for SQL Server and it's very important keep it always updated Steps and Notes Using ADO.NET Entity Data Model I chose Code First from Database ( new feature in EF 6.1 ) to generate the models. Note: Model classes and DbContext class generated successfuly but NO .edmx or .tt file was generated . Next I added a new scaffold item: MVC 5 Controllers with views, using Entity Framework. Note: Success, controllers and views generated Question From now on I don't want to use Code First to update my

When is the Seed method called in a EF code first migrations scenario?

我只是一个虾纸丫 提交于 2019-11-29 16:37:06
问题 I'm new in a project and there is this class for the seed data: internal sealed class Configuration : DbMigrationsConfiguration<DAL.Context> { public Configuration() { AutomaticMigrationsEnabled = true; } And this code to start the seed: protected override void Seed(Context context) { try { My question is: when is the Seed method called? Only when a user does update-database and the user doesn't have the database (basicly a new user), or also when the user with an existing database calls an

Entity Framework database mapping relationships (Duplicate creation using Seed() method)

你。 提交于 2019-11-29 16:21:16
I created a post with an issue and another issue . These can be looked at for references but i consider them as handled. My question arising from these issues and the action i (need or not need) to apply bothers me because i don't quite understand EF its behavior and expectations. I have a Product, PurchasePrice and SalesPrice entity where my initial thought was that 1 Product can have multiple PurchasePrices but that 1 PurchasePrice only can exist in 1 Product (same for SalesPrice). Therefore these relations: // NOTE that BaseEntity just has a "int ID" prop and datetimes/stamps public class

Entity Framework 6 Custom Relationship Convention

筅森魡賤 提交于 2019-11-29 16:07:32
I have read this documentation about convention in Entity Framework 6. But it does not contain convention for Relationship. Suppose I have following model: [TablePrefix("mst")] public class Guru { public int Id { get; set; } public int? IdKotaLahir { get; set; } public virtual Kota KotaLahir { get; set; } } I want property IdKotaLahir to be foreign key of navigation property KotaLahir . Foreign key name is "Id"+<NavigationPropertyName> . Is it possible using current version of entity framework (EF 6 alpha 3)? Is it just one property or you need this across the board (i.e. the whole model is

Updating child objects in Entity Framework 6

为君一笑 提交于 2019-11-29 15:54:04
问题 (Using Entity Framework 6.2) I have the following two models/entities: public class City { public int CityId { get; set; } public string Name { get; set; } } public class Country { public Country() { Cities new HashSet<City>(); } public int CountryId { get; set; } public string Name { get; set; } public virtual ICollection<City> Cities { get; set; } } And the following DbContext public DbSet<Country> Countries { get; set; } My question is: If the children of the Country object change (i.e.

Sql Stored proc and Entity framework 6

风流意气都作罢 提交于 2019-11-29 15:46:14
Suppose i have a one parameter stored procedure : procedure dbo.MyStoredProc @param VARCHAR(50) as SELECT * FROM whatever WHERE column = @param In my web application (mvc with entity 6 ) i wish to call my stored procedure, and retreive all the line from it's SELECT statement. var DBLines = MyStoredProc(parameterValue); I saw something disappointing but very logic; my var was containing an integer. That integer was 1. It was telling me that everything went ok in my stored procedure... I want that var to contains every SQL lines my stored proc went through . I suppose it is possible but how ?