ef-code-first

How do I add a reference to another existing entity using code-first framework without first pulling the existing entity from the database

我们两清 提交于 2019-12-25 03:46:05
问题 I'm trying to attach an existing product to an auction, but am unable to do so without first pulling the product from the database. This code works, but how would I go about just providing the productid and then saving the auction var product = new Product() { //Known existing product id ProductId = 1 }; var auction = new Auction { BuyItNowPrice = 10. Product = product, ... ... }; using (var db = new DataContext()) { var product = db.Products.Find(auction.Product.ProductId); auction.Product =

Entity Framework Code First - Default values on non-nullable types

一世执手 提交于 2019-12-25 03:37:15
问题 i'm trying to map a legacy database with a legacy POCO model. Since database and model where develop not having in mind Entity Framework there are some little differences between that made me stuck. The challenge I'm facing is that I would like to make it work being as less invasive as possible (don't want to touch code of the database nor the model) since there is too much codes that depends on. I have tried to map the entities using a code first approach, reusing the POCOs from the legacy

Entity and mapping using Entity Framework 4 how to handle null (ICollection)?

纵饮孤独 提交于 2019-12-25 03:21:04
问题 I have this fellowing entity : public class Post { public long PostId { get; private set; } public DateTime date { get; set; } [Required] public string Subject { get; set; } public User User { get; set; } public Category Category { get; set; } [Required] public string Body { get; set; } public virtual ICollection<Tag> Tags { get; private set; } public Post() { Category = new Category(); } public void AttachTag(string name, User user) { if (Tags.Count(x => x.Name == name) == 0) Tags.Add(new

ProviderIncompatibleException when running first time. But, not in second time

[亡魂溺海] 提交于 2019-12-25 02:59:45
问题 So, basically I'm using EF with Reverse POCO Code First Generator in my project. My connection Strings look like this: <connectionStrings> <add name="HistoryDBContext" connectionString="" providerName="System.Data.SqlClient" /> <add name="ProjectMgtContext" connectionString="data source=xxx;initial catalog=xxx;user id=xx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings> The idea is that I am going to read the

The operation failed because an index or statistics with name 'IX_ID' already exists on table 'TestAs'?

空扰寡人 提交于 2019-12-25 01:17:48
问题 I'm using EF4.3.1 code first, I have the code as below, namespace ConsoleApplication5 { class Program { static void Main(string[] args) { using (var context = new myContext()) { TestA ta = new TestA(); ta.Name = "Hero"; TestB tb = new TestB(); tb.Name = "Allen"; TestC tc = new TestC(); tc.Name = "Iverson"; ta.tb = tb; ta.tc = tc; context.testASet.Add(ta); } } } class TestA { public int ID { get; set; } public string Name { get; set; } public TestB tb { get; set; } public TestC tc { get; set;

Entity framework code first - projection problems

心不动则不痛 提交于 2019-12-24 21:56:11
问题 I have something like this: var model = forumsDb.Categories .Select(c => new {c, c.Threads.Count}) .ToList() My Category object looks like this (pseudocode): public class Category { public int id {get;set;} public ICollection<Thread> Threads { get; set; } /***some properties***/ [NotMapped] public int ThreadCount {get;set;} } Now, in my model object, i have two items: model.c and model.Count . How can i map model.Count into model.c.ThreadCount ? 回答1: Define a strong type: public class

Invalid Object Name error when creating database with EF Code First

巧了我就是萌 提交于 2019-12-24 19:15:32
问题 Hello i am developing a database using EF Core Code First .I keep getting this error when generating the database : Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.' InnerException {"Invalid object name 'Parents'."} System.Exception {Microsoft.Data.SqlClient.SqlException} From what i have read it seems i can not create the database because the tables are somehow pluralized. I have searched for solutions

Storing objects with Code First that are related as classes and subclasses

纵饮孤独 提交于 2019-12-24 19:07:21
问题 As an example I have an object Person, and Person is inherited by Man and Woman. Person contains the properties that both Man and Woman share, and Man and Woman contain the specific properties. What I wanna do is store those in a database using Entity Framework 4.1 Code First. So I made three model objects class Person { int Id { get; set; } string Name { get; set; } string Weight { get; set; } public virtual Woman Woman { get; set; } public int WomanID { get; set; } public virtual Man Man {

HttpRuntime.Cache and Service Layer

怎甘沉沦 提交于 2019-12-24 18:26:06
问题 I have Entity Framework level-2 caching classes in seprate project named Cache Layer,which use HttpRuntime.Cache for caching, Now i want use that in my Service Layer for caching EF Code First Database results. There is Coupling between HttpRuntime and Service Layer? If yes, How i can use HttpRuntime.Cache in Service Layer without coupling? 来源: https://stackoverflow.com/questions/14853827/httpruntime-cache-and-service-layer

HttpRuntime.Cache and Service Layer

柔情痞子 提交于 2019-12-24 18:24:54
问题 I have Entity Framework level-2 caching classes in seprate project named Cache Layer,which use HttpRuntime.Cache for caching, Now i want use that in my Service Layer for caching EF Code First Database results. There is Coupling between HttpRuntime and Service Layer? If yes, How i can use HttpRuntime.Cache in Service Layer without coupling? 来源: https://stackoverflow.com/questions/14853827/httpruntime-cache-and-service-layer