ef-code-first

Guidance for synchronising reverse associations in Entity Framework 4.1

≯℡__Kan透↙ 提交于 2019-12-17 18:58:09
问题 EF 4.1 synchronises reverse associations when you create your instances. Is there any documentation of or best practices guidance available for this behaviour? What I mean by synchronising the reverse association is that given: public class Blog { public Blog() { Posts = new List<Blog>(); } public int Id { get; set; } public ICollection<Post> Posts { get; private set; } } public class Post { public Blog Blog { get; set; } public int Id { get; set; } } Then after the following line the Post

Returning a DataTable using Entity Framework ExecuteStoreQuery

前提是你 提交于 2019-12-17 18:54:44
问题 I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical. Is it possible and how would I return a DataTable using ExecuteStoreQuery ? public ObjectResult<DataTable> MethodName(string fileSetName) { using (var dataContext = new DataContext(_connectionString)) { var returnDataTable = ((IObjectContextAdapter)dataContext).ObjectContext.ExecuteStoreQuery<DataTable>("SP_NAME","SP_PARAM"); return returnDataTable;

Update existing database with Entity Framework Code First in MVC

谁都会走 提交于 2019-12-17 18:43:23
问题 In my MVC application I used Entity Framework 6 and created database with code first approach. After a certain time, I updated one of the entity classes by adding new column and removing some columns. For reflecting these changes to the database I followed the steps below: Deleted the migrations folder in the project. Deleted the __MigrationHistory table in the database. Then run the following command in the Package Manager Console: Enable-Migrations -EnableAutomaticMigrations -Force Add the

Entity Framework 4 Code First - Prevent DB Drop/Create

倾然丶 夕夏残阳落幕 提交于 2019-12-17 18:39:57
问题 I've written an ASP.Net MVC 3 application using the Code First paradigm whereby when I make a change to the model the Entity Framework automatically attempts to re-create the underlying SQL Server Database via DROP and CREATE statements. The problem is the application is hosted on a 3rd party remote server which limits the number of databases I can have and does not seem to allow me to programmatically execute "CREATE DATABASE..." statements as I gather from this error message: CREATE

How can I prevent EF “The context cannot be used while the model is being created” errors?

安稳与你 提交于 2019-12-17 18:22:04
问题 Looking at my Elmah error logs, I am seeing a few InvalidOperationException s from Entity Framework that deal with: The context cannot be used while the model is being created. This is with the latest EF CodeFirst library from Nuget. The only information I have been able to find on the net is that it is being caused by having data contexts as singletons, which is most certainly not my case. In my Windsor installer, my EF unit of work structure is being registered with: container.Register

Entity Framework: How to avoid Discriminator column from table?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 18:04:10
问题 I have the following table created using Entity Framework Code First approach. How do I modify the C# code so that the unwanted Discriminator column is not created in the database? Are there any attributes to achieve this? How do I make the foreign key column named PaymentID instead of Payment_ PaymentID ? Are there any attributes to achieve this? Note: Runtime version for EntityFramework.dll is v4.0.30XXX . CODE public abstract class PaymentComponent { public int PaymentComponentID { get;

EF Code First foreign key without navigation property

巧了我就是萌 提交于 2019-12-17 17:38:37
问题 Let's say I have the following entities: public class Parent { public int Id { get; set; } } public class Child { public int Id { get; set; } public int ParentId { get; set; } } What is the code first fluent API syntax to enforce that ParentId is created in the database with a foreign key constraint to the Parents table, without the need to have a navigation property ? I know that if I add a navigation property Parent to Child, then I can do this: modelBuilder.Entity<Child>() .HasRequired

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

ぃ、小莉子 提交于 2019-12-17 17:29:13
问题 I have created a new clean asp.net 5 project (rc1-final). Using Identity Authentication I just have the ApplicationDbContext.cs with the following code: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { protected override void OnModelCreating(ModelBuilder builder) { // On event model creating base.OnModelCreating(builder); } } Please note ApplicationDbContext use IdentityDbContext and not DbContext. There is any IdentityConfig.cs. Where i need to put the classic

Delete .mdf file from app_data causes exception cannot attach the file as database

寵の児 提交于 2019-12-17 17:25:12
问题 I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdf file after I changed the model, I manually deleted the file from the app_data directory in VS. I have done this a few times before without any problem. Now I receive an exception: The underlying provider failed on Open. ==> Cannot attach the file MYDB.mdf as database 'MYDB' . I will appreciate your help on how to recreate the .mdf file. Thank you! 回答1: Use SQL Server Object Explorer to delete the

Unhandled Exception after Upgrading to Entity Framework 4.3.1

℡╲_俬逩灬. 提交于 2019-12-17 16:46:39
问题 Error: Unhandled Exception: System.Data.SqlClient.SqlException: The operation failed because an index or statistics with name 'IX_ID' already exists on table 'PrivateMakeUpLessons'. Model (Simplified, building in a separate test project for debugging): public abstract class Lesson { public Guid ID { get; set; } public string Room { get; set; } public TimeSpan Time { get; set; } public int Duration { get; set; } } public abstract class RecurringLesson : Lesson { public int DayOfWeek { get; set