entity-framework-4.1

EF CF: many-to-many relation with additional info

会有一股神秘感。 提交于 2019-12-07 14:08:28
问题 We have legacy database and we map the new objects and props to the old tables and columns. So far so good. We have many-to-many relation which was mapped successfully. The intermediate table contains additional data. When we try to map the intermediate table to an object we get exception that the mapping is already defined. If we remove mapping from any side of the relation we get error that table is missing (ofc, we expect just that). I can do that easily with NHibernate and I am starting

.net mvc razor dropdowns for foreign keys

我的梦境 提交于 2019-12-07 13:42:31
Ok, this seemed simple, but is making my head spin. I have created models based on CodeFirst. public class Category { public int ID { get; set; } [StringLength(255, MinimumLength = 1)] public string Name { get; set; } } public class SubCategory { public int ID { get; set; } public Category category { get; set; } [StringLength(255, MinimumLength = 1)] public string Name { get; set; } } Now when i auto-generate the controller and view for SubCategory it (out-of-the-box) lets me create new SubCategory objects without specifying the Category (even through it correctly creates a foreign key

Querying an Entity's child collections in Entity Framework 4.1

寵の児 提交于 2019-12-07 13:33:46
问题 I have a set of entities that I have queried from IDbSet in my DbContext. I now want to iterate over each entity and query their child collections, which are defined in the entity as ICollection. Is it correct to call AsQueryable() on the child collections and run my linq query on that? If so, will my queries be linq-to-objects or does the collection object populated by EF implement IQueryable that goes to the database? Thanks for any insight on this. 回答1: It whole depends on how your

Does LINQ to Entities support IEquatable in a 'where' clause predicate?

ぃ、小莉子 提交于 2019-12-07 12:32:19
问题 I have a simple base entity type called EntityBase that implements IEquatable<EntityBase>. I've used this code outside of LINQ to Entities (with EF 4.1) to filter a list of types derived from EntityBase using a predicate where clause that compares the object instances rather than the ID property they inherit from EntityBase. So basically I want to do this: UserAccount account = GetMyNewAccount(); Accounts.Where(item => item == account).SingleOrDefault(); rather than this: UserAccount account

ASP.Net MVC 3 EF “Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths”

試著忘記壹切 提交于 2019-12-07 12:03:57
问题 I am creating an ASP.Net MVC 3 application and I am running into a foreign key constraint problem when trying to update my database using migrations. I am using Code-First, and the error I am getting is: Introducing FOREIGN KEY constraint 'FK_CategoryItemValues_CategoryProperties_CategoryPropertyId' on table 'CategoryItemValues' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint.

What does a repository look like when using many 1:Many or Many:Many tables?

血红的双手。 提交于 2019-12-07 11:48:07
问题 I have a MVC application that uses that entity framework model object directly in the view. I'm not sure how bad of a design pattern this is, but based on this sample I need to use a repository to implement caching. namespace MVCAzureStore.Services.Caching { public class CachedProductsRepository : CachedDataSource, IProductRepository { private readonly IProductRepository repository; public CachedProductsRepository(IProductRepository repository, ObjectCache cacheProvider) : base(cacheProvider,

Development and Production databases

淺唱寂寞╮ 提交于 2019-12-07 11:32:18
问题 I and my team are currently doing a project, where we are using Entity Framework 4.1 (Code First). We want to write some tests, but we don't want them to run on our primary database, as we have a team in Singapore writing a client for what we do, and they are hitting that database constantly. So to avoid disturbance when running our tests, we would like to have a different database for testing. How do we handle a second database when using Entity Framework? We want a solution that is semi

How do I pass a connection string to the constructor of a database-first DBContext with Entity Framework 4.1?

我怕爱的太早我们不能终老 提交于 2019-12-07 10:08:47
问题 For various reasons I would like to not store the connection string for my Entity Framework DB model in one of the various .config files. (I am using the latest and greatest DBContext API with the new Entity Framework version 4.1, .NET 4 and Visual Studio 2010 C#.) However, the code generation template for DBContext only creates a single parameterless constructor. (If I don't use the DBContext API, then my entity framework model has 7 different constructors to chose from, including the one I

which way? Database-first , model-first , Code-Only?

风流意气都作罢 提交于 2019-12-07 10:03:08
问题 recently I learned Entity Framework by Pro Entity Framework 4.0 book... Now,I want to write the project by EF ...With these conditionsو which way is better and more flexible? My opinion is the Model-first! however I want to know your opinion? thanks! 回答1: Read this: EF 4.1. Which way to go? It goes through the permutations based on your preferences and situation. My opinion mimics what is stated. If you don't have an existing database and dislike visual designers, code first is for you. If

Entity Framework 4.1 code-first, required lazy load reference is null on save

泪湿孤枕 提交于 2019-12-07 09:05:31
问题 I'm building a forum project for ASP.NET MVC 3, running on .NET 4 with the latest version of Entity Framework. I have the usual forum design, with a Board with Categories, and Categories with Forums, Forums with Topics and Topics with Posts etc. Simplified: public class Category { [Required] public virtual Board Board { get; set; } } public class Forum { [Required] public virtual Category Category { get; set; } } public class Topic { [Required] public virtual Forum Forum { get; set; } }