entity-framework-5

Onion Architecture- Entity Framework Code First Models DataAnnotations

泪湿孤枕 提交于 2019-12-17 18:43:51
问题 I am developing a ASP.NET MVC Project following the Onion Architecture. I have added the Models inside my Core Project and these Models will be referred as the POCO classes for the Entity Framework Models in the Infrastructure Project. My question is how can I add Data Annotations Which depends on the Entity Framework? Can I make the Core Models as Interfaces and inherit it in the Infrastructure Projects and do real Implementation? 回答1: You don't need to create Core Models as Interfaces if

DbContext has been disposed and autofac

别来无恙 提交于 2019-12-17 18:07:24
问题 I have a controller: private readonly ILogger _logger; private readonly IRepository _repository; public HomeController(ILogger logger, IRepository repository) { _logger = logger; _repository = repository; } This is the repository: public class EfRepository : IRepository { // ...methods for add, delete, update entities // .... public void Dispose() { if (this._context != null) { this._context.SaveChanges(); (this._context as IDisposable).Dispose(); this._context = null; } } } Finally,

Debugging Package Manager Console Update-Database Seed Method

为君一笑 提交于 2019-12-17 17:26:24
问题 I wanted to debug the Seed() method in my Entity Framework database configuration class when I run Update-Database from the Package Manager Console but didn't know how to do it. I wanted to share the solution with others in case they have the same issue. 回答1: Here is similar question with a solution that works really well. It does NOT require Thread.Sleep . Just Launches the debugger using this code. Clipped from the answer if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics

How to set NewId() for GUID in entity framework

烈酒焚心 提交于 2019-12-17 16:31:10
问题 I am creating asp.net mvc4 sample.In this i created Id column as GUID in Sample table of datacontext. public class Sample { [Required] public Guid ID { get; set; } [Required] public string FirstName { get; set; } } This is entity table CreateTable( "dbo.Samples", c => new { ID = c.Guid(nullable: false), FirstName = c.String(nullable: false) }) .PrimaryKey(t => t.ID); Id pass 00000000-0000-0000-0000-000000000000. How to set newid() to GUID and where i have to set. 回答1: I would recommend just

Entity Framework Code First Migrations: Set Primary Key Value

前提是你 提交于 2019-12-17 16:30:13
问题 I have a table that stores some extra data for some rows of a table like: public class QuoteExtra { [Key] public int QuoteId { get; set; } // More fields here } I'd like to be able to add rows to this table where I explicitly set the PK. If I simply leave it as above, setting a value and submitting the row causes the value to be discarded and replaced with the auto-generated value from the database (and the column is defined as an Identity column in the actual schema). This appears to be the

Enums EF 5.0 - Database First

允我心安 提交于 2019-12-17 15:54:25
问题 How can I make it so that my context objects uses the Enum feature in Entity Framework 5.0 if I am using Database First. 回答1: Go to the model browser and create a new enum type, then go to whatever column you wish to use it on and change its type to the enum that you just created. 来源: https://stackoverflow.com/questions/11595008/enums-ef-5-0-database-first

Upgrade from Entity Framework 5 to 6

二次信任 提交于 2019-12-17 15:26:10
问题 After upgrading our project from using Entity Framework 5 to Entity Framework 6 (though NuGets update function) i get the following error on my generated Entities class: Error 1 The type or namespace name 'Objects' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) I understand that this is because the namespace has changed and i can manually fix the error by changing my imports from: using System.Data.Objects; and using System.Data.Objects.DataClasses; To:

DbContext AutoDetectChangesEnabled set to false detecting changes

百般思念 提交于 2019-12-17 10:34:46
问题 I'm a bit stumped. From what I've read setting the DbContext.AutoDetectChangesEnabled to false should disable change tracking requiring one to call DbContext.DetectChanges in order to identify changes to be sent to the database. However, it is clear from my logs below that the changes are being registered by dbContexts change tracker, even with the setting set to false. Am I missing something? Entity Framework Version: 5.0.0.0 DbContext class public class ProjectContext : DbContext { public

how to create an audit trail with Entity framework 5 and MVC 4

泄露秘密 提交于 2019-12-17 10:25:30
问题 I am building an MVC 4 application, using EF 5. I need to do an audit trail, ie log any changes that end users make. I have asked this question a few times, but haven't really gotten a satisfying answer before. So I am adding a lot more details in hoping to get somewhere.. currently I have multiple repositories ie public class AuditZoneRepository : IAuditZoneRepository { private AISDbContext context = new AISDbContext(); public int Save(AuditZone model, ModelStateDictionary modelState) { if

Entity Framework Code First Using Guid as Identity with another Identity Column

两盒软妹~` 提交于 2019-12-17 08:16:30
问题 a.k.a How can we create multiple identity columns in Code First? Because of clustering performance, a common recommendation is to use an autoincremented integer column instead of a GUID created with newid() . In order to declare a column as autoincrement, you have to specify it with the Annotation [DatabaseGenerated(DatabaseGeneratedOption.Identity)] . But, you can only have one identity in a table. So starting with a base model like: public abstract class ModelBase { // the primary key