code-first

Code First Migration with Connection Strings

丶灬走出姿态 提交于 2019-11-27 11:30:15
问题 So I've managed to get Code First running and it works great. Since I am still developing the application the structure of the Database isn't finalize and so I need to implement migrations. I followed the Official Blog Post and got that Update-Database command working. However, this only updates the SQLExpress version of the database. The production version of the database is on Azure and I specify the connection string at run time so the Update-Database command doesn't work on that. So my

Database in use error with Entity Framework 4 Code First

时光毁灭记忆、已成空白 提交于 2019-11-27 11:06:01
I have an MVC3 and EF 4 Code First application, which is configured to change the DB when the model changes, by setting the DB Initializer to a DropCreateDatabaseIfModelChanges<TocratesDb> , where TocratesDb is my derived DbContext . I have now made a change to the model, by adding properties to a class, but when EF tries to drop and recreate the DB, I get the following error: Cannot drop database "Tocrates" because it is currently in use. I have absolutely no other connections anywhere open on this database. I assume that my cDbContext still has an open connection to the database, but what

How can I disable code first migrations

余生颓废 提交于 2019-11-27 11:04:23
I have a code-first entity model in EF5. But I want to manage the database changes manually -- I do not want EF to modify my existing database and all its data. But when I make parallel changes in the EF mapping and in the database, EF refuses to operate properly telling me I need to use code first migration. How do I turn this off? set the Database.SetInitializer to null. public class DatabaseContext: DbContext { //the base accepts the name of the connection string provided in the web.config as a parameter public DatabaseContext() : base("DatabaseContext") { //disable initializer Database

EF Code First Parent-Child insertions with identity columns

别来无恙 提交于 2019-11-27 10:51:04
问题 i have the following model. class Parent { int ParentId (identity column) { get; set; } string ParentName { get; set; } virtual ICollection<Child> Children { get; set; } } class Child { int ChildId (identity column) { get; set; } string ChildName { get; set; } int ParentID { get ; set; } //foreign key to Parent(ParentID) } How do i insert few rows to my parent and child in single transaction? Basically i want to get the identity generated on the parent(say i insert a row in parent) and insert

Adding CreatedDate to an entity using Entity Framework 5 Code First

筅森魡賤 提交于 2019-11-27 10:31:28
问题 I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UTC date. I do NOT want to use a constructor, as I have many entities in my model that I want to inherit from an abstract class containing the CreatedDate property, and I can't enforce a constructor with an interface. I have tried different data annotations and I have attempted to write a database initializer that would pick up a

Entity Framework Code First & Search Criteria

▼魔方 西西 提交于 2019-11-27 10:11:58
问题 So I have a model created in Entity Framework 4 using the CTP4 code first features. This is all working well together. I am attempting to add an advanced search feature to my application. This "advanced search" feature simply allows the users to enter multiple criteria to search by. For example: Advanced Product Search Name Start Date End Date This would allow the user to search by the product name and also limit the results by the dates that they were created. The problem is that I do not

Oracle.ManagedDataAccess.EntityFramework - ORA-01918: user 'dbo' does not exist

痞子三分冷 提交于 2019-11-27 09:12:43
I am trying to implemente code First Migrations with Oracle.ManagedDataAccess 6.121.1.0 provider, but with no success at all. As I am receiving a ORA-code, I am assuming that the connection are been opened successfully. But the Migrations are failing because, maybe, the provider are behaving as a SQL Server, instead of Oracle. I think that beacause it is traying to use 'dbo' as default schema. Here is my web.config settings: <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0,

How can I get my database to seed using Entity Framework CodeFirst?

白昼怎懂夜的黑 提交于 2019-11-27 09:09:26
问题 The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions? On a side note, is it possible to call the initializer without having a reference to my DatabaseContext in the client? I have included all the relevant code I could think of. If anything else would be helpful, please let me know. Things I've Tried: I deleted my connection string (since it defaults to sqlexpress

How to disable migration in Entity Framework 4.3.1?

▼魔方 西西 提交于 2019-11-27 08:13:13
Is there any way to disable migration in Entity Framework 4.3.1? I removed the migrations folder from the project and the generated tables in my database, but it doesn't work! How can you remove the migration? Ladislav Mrnka If you don't want to use migrations but in the same time you want EF to create database for you, you just need to set correct database initializer: Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists<YourContentType>()); Noel Deleting the Migrations folder has worked for me. I don't get any errors, it puts me back to where I started. Buzzrick The way

Fluent API, many-to-many in Entity Framework Core

感情迁移 提交于 2019-11-27 07:06:16
I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet<Club>(); } public int PersonId { get; set; } public virtual ICollection<Club> Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet<Person>(); } public int ClubId { get; set; } public virtual ICollection<Person> Persons { get; set; } } Please correct me if im wrong but I could honestly not find a question that contains an elaborate explanation on