entity-framework-6

Navigation properties not loading properly

白昼怎懂夜的黑 提交于 2019-12-21 09:21:37
问题 My context looks like: public class ApplicationDbContext: IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { this.Configuration.LazyLoadingEnabled = true; } //DbSet properties } so, lazy loading is enabled. I have following class: public class Home { private ICollection<Slide> _slides; [Key] [Required] public string Name { get; set; } [ForeignKey("Header")] public int? HeaderID { get; set; } //Navigation properties public ICollection<Slide> Slides

Merging migration entries in Entity Framework

*爱你&永不变心* 提交于 2019-12-21 09:09:15
问题 I have an Entity Framework 6 CF project that has a few migrations already in place. The model is now stable and there is no need to keep the migration history that already exists. Is there a way to reset the model and merge all migration commands into the initial migration? As an example, the first migration adds a column while the second migration adds a unique, non-clustered index. I now want to see all these changes directly in OnModelCreating rather than in separate migrations. 回答1:

How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?

笑着哭i 提交于 2019-12-21 07:55:24
问题 I have my database with table names starting with "tbl" prefix and column names like "ua_id" which are understandable in context of project but problematic if used in a model i.e names should be readable or meaningful(not like indicative names defined in database). So I want to map them in my onmodelcreating method but I have no idea about it. I studied it in following blog: http://weblogs.asp.net/scottgu/entity-framework-4-code-first-custom-database-schema-mapping but this is for EF 4.1 and

Entity Framework 6 & TPH inheritance: Map properties with the same name to same column by default

有些话、适合烂在心里 提交于 2019-12-21 04:23:16
问题 As of EF6 it is possible to do something like this when configuring Entity mappings using Table Per Hierarchy inheritance: public class MyContext : DbContext { public DbSet<Device> Devices { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<ABatteryPoweredDevice>().Property(c => c.BatteryLevel).HasColumnName("BatteryLevel"); modelBuilder.Entity<ADifferentBatteryPoweredDevice>().Property(c => c.BatteryLevel).HasColumnName("BatteryLevel"); }

How to execute SqlQuery with Entity Framework Core 2.1?

房东的猫 提交于 2019-12-21 03:37:07
问题 In Entity Framework 6, I can execute a raw SQL query on the database using the following command: IEnumerable<string> Contact.Database.SqlQuery<string>("SELECT a.title FROM a JOIN b ON b.Id = a.aId WHERE b.Status = 10"); On a new project, I am trying to use Entity Framework Core 2.1. I have a need to execute raw SQL query. While googling, I can see that the extension SqlQuery was changed to FromSql . However, FromSql only exists on the DbSet<> not on the DbContext.Database . How can I run

Connecting to SQL Server with EF6

好久不见. 提交于 2019-12-20 20:34:22
问题 Up to EF5, in order to connect to SQL Server 2012, all I needed to to is specify a connection string that looks something like this: Data Source=.\SqlExpress;Initial Catalog=MyDatabase;Integrated security=True;MultipleActiveResultSets=True This method is not working with EF6, giving exception No Entity Framework provider found for 'System.Data.Odbc' ADO.NET provider. Make sure the provider is registered in the 'entityFramework' section of the application config file I am not using app.config

WEB API JSON Serializing Circular References

早过忘川 提交于 2019-12-20 18:36:30
问题 I'm trying to access the property fields (JSON) from the child entity that is received from Web API. By looking at the browser console, however, it is showing a reference instead of the fields. How do I get access to these fields? ANGULAR JS VIEW <table infinite-scroll='tF.loadMore()' infinite-scroll-disabled='tF.isBusy' infinite-scroll-distance='3' class="responsive"> <thead> <tr> <th>FIELD 1</th> <th>FIELD 2</th> <th>FIELD 3</th> <th>FIELD 4</th> <th>FIELD 5</th> </tr> </thead> <tbody> <tr

WEB API JSON Serializing Circular References

时光总嘲笑我的痴心妄想 提交于 2019-12-20 18:36:22
问题 I'm trying to access the property fields (JSON) from the child entity that is received from Web API. By looking at the browser console, however, it is showing a reference instead of the fields. How do I get access to these fields? ANGULAR JS VIEW <table infinite-scroll='tF.loadMore()' infinite-scroll-disabled='tF.isBusy' infinite-scroll-distance='3' class="responsive"> <thead> <tr> <th>FIELD 1</th> <th>FIELD 2</th> <th>FIELD 3</th> <th>FIELD 4</th> <th>FIELD 5</th> </tr> </thead> <tbody> <tr

WEB API JSON Serializing Circular References

和自甴很熟 提交于 2019-12-20 18:36:00
问题 I'm trying to access the property fields (JSON) from the child entity that is received from Web API. By looking at the browser console, however, it is showing a reference instead of the fields. How do I get access to these fields? ANGULAR JS VIEW <table infinite-scroll='tF.loadMore()' infinite-scroll-disabled='tF.isBusy' infinite-scroll-distance='3' class="responsive"> <thead> <tr> <th>FIELD 1</th> <th>FIELD 2</th> <th>FIELD 3</th> <th>FIELD 4</th> <th>FIELD 5</th> </tr> </thead> <tbody> <tr

How do I go about unit testing with Entity Framework and Moq?

情到浓时终转凉″ 提交于 2019-12-20 15:22:20
问题 I'm new to Moq, and wanting to use it like a backing store for data - but without touching the live database. My setup is as follows: A UnitOfWork contains all repositories, and is used for data access throughout the application. A Repository represents a direct hook into a DbSet, provided by a DbContext. A DbContext contains all DbSets. Here is my test so far: // ARRANGE var user = new User() { FirstName = "Some", LastName = "Guy", EmailAddress = "some.guy@mockymoqmoq.com", }; var mockSet =