ef-code-first

Entity Framework 4.1 Code First - Controlling Eager Loading

为君一笑 提交于 2019-12-13 02:27:24
问题 Is it possible to control eager loading of child objects. If I had a parent class that has 20,000 child objects and I only wanted to retrieve a subset of those child objects is it possible? How would I write the query to do that if it is? For Example: I have an Entity called Book which has a number of related Reviews: public class Book { public int BookId { get; set; } public string BookName { get; set; } public ICollection<Review> Reviews { get; set; } } public class Review { public int

Ignore some inherited properties in EF6 code first mapping(.NET4 not .NET4.5)

蓝咒 提交于 2019-12-13 02:26:55
问题 I'm using EF6 code first with .NET4(I should deliver the project on win xp so I couldn't configure it with .NET4.5) in a win Form project. I have a BaseEntity class that all other entities inherited from it: public abstract class BaseEntity { public int Id {get; set;} public int X {get; set;} public int Y {get; set;} } public class Calendar:BaseEntity { // properties } How could I Ignore X,Y properties in my all entities without writing following code for each entity? modelBuilder.Entity

mvc connection string code first

荒凉一梦 提交于 2019-12-13 01:19:14
问题 im new to mvc and im trying out some stuff with code first. im currently having problems with my dbase, whenever i build my application the build finishes successfully but i dont see my dbase get created whenever i check my local server. was wondering if there's something im missing on my connection string. <connectionStrings> <add name="mydbase" connectionString="Data Source=localhost;Database=mydbase;Trusted_Connection=yes;" providerName="System.Data.SqlClient" /> </connectionStrings> or

When exactly does Entity Framework Fire a SQL Command?

丶灬走出姿态 提交于 2019-12-13 00:49:21
问题 Suppose I have code like: public void TestWhenSqlFires() { var db = new Db(); // EF DbContext var items = db.SimpleObjects; // SimpleObject implements interface IId var byId = WhereId(items, 1); var array = byId.ToArray(); } protected IEnumerable<IId> WhereId(IEnumerable<IId> items, int id) { return items.Where(i => i.Id == id); } At what line in TestWhenSqlFires() will SQL actually be run against the database? (This is a question that spun off from comments on this answer) 回答1: One way to

EF Core Include multiple inherited types TPH

依然范特西╮ 提交于 2019-12-13 00:14:39
问题 The problem Query BaseClass and Include information from derived types. For example: Base class Offer has two types one with a Card only ( CardOffer ) and with Multiple Cards ( CarouselOffer ). Question: How to query the BaseClass and Include the information from the derived types CardOffer and CarouselOffer? In the manner shown below: _dbContext.Offers.Include(...).ToList(); Currently I solved this by querying each inherited type var cards = await _dbContext.CardOffers .Include(c => c.Card)

Code First can't map an override property

a 夏天 提交于 2019-12-12 20:14:48
问题 I have problems mapping a overrided property from a POCO object to database using TPH in Code First. My code is similar to these classes: public abstract class Vehicle { public int ID { get; set; } public abstract NumberOfWheels { get; set;} } public class Motorbike: Vehicle { public override NumberOfWheels { get; set; } } When I try to save the Motorbike class to Database I get the next message The property 'NumberOfWheels' is not a declared property on type 'Motorbike'. Verify that the

Many to many relationship update: Cannot insert duplicate key

≡放荡痞女 提交于 2019-12-12 19:55:47
问题 I am still new in EF code first, so please be lenient with me. I have these entity classes: public class User { public int UserId { get; set; } public string UserName { get; set; } public string EmailAddress { get; set; } public string Password { get; set; } public virtual ICollection<Task> Tasks { get; set; } public virtual ICollection<Task> TaskAssignees { get; set; } public User() { Tasks = new List<Task>(); } } public class Task { public int TaskId { get; set; } public string Name { get;

Unable to apply code first migrations to mysql database

霸气de小男生 提交于 2019-12-12 19:23:09
问题 I am working on asp.net mvc with EF code first model. I am trying to apply migrations using EF code first to my project. I am using MySql database. currently i am using EF 4.3.1 version and mysql connector/.net of 6.6.4.0 version. I am successfully add migrations to my project that means i execute series of commands that gave no errors for me. I have followed these steps, PM> Enable-Migrations PM> Add-Migration InitialMigration PM> update-database -verbose these steps create Migration folder

Entity Framework 6 Code First int Identity column value is zero for first row

ε祈祈猫儿з 提交于 2019-12-12 19:17:33
问题 After the first execution of the update-database command to populate the database with seed data: Found that all int Id columns started with zero (0) rather than the expected one (1). Added the following 2 lines of code for each entity/table to the top of the Seed method in Configuration.cs: [Note: Because of the foreign key constraints, I deleted all rows in descendent tables, and then worked my way up the ancestral chain.] context.Database.ExecuteSqlCommand("delete from Widgets"); context

Entity Framework 4.1 Code First - Computed/Calculated column not being inserted

自作多情 提交于 2019-12-12 18:39:34
问题 I have below entity with a calculated/computed column: public EntityA { [Key(), Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } ..... [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public virtual string RefId { get { return this.Id.ToString().PadLeft(7, '0'); } private set { } } } RefId is a computed column that depends on the Id value. After performing commit changes to database with SaveChanges I can check that Id and RefId have been set