entity-framework-4.1

.net mvc razor dropdowns for foreign keys

丶灬走出姿态 提交于 2019-12-08 04:48:35
问题 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

EF CTP5 - Context Inheritance Across Multiple Assemblies

巧了我就是萌 提交于 2019-12-08 04:39:20
问题 I have two assemblies, each with models and a model context. The first assembly model context is derived from DbContext. The second assembly model context is derived from the first assembly model context. This works, except the database generation fails because the first assembly models aren't considered when generating the database. Is there a way to ensure that the first assembly models are properly considered during database generation? 回答1: I solved this by loading the other assembly's

Managing connections with Generic Repository pattern

耗尽温柔 提交于 2019-12-08 04:37:58
问题 I am building a site using entity framework 4.1 and mvc3. I am using the Generic Repository pattern: http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle. I use ninject to inject my concretes repositories to the controllers. My problem is that each of my dbcontext different and I cannot run complex queries without the code shouting something like "different contexts cannot be used in the same query". I tried using the

Define relationships for hierarchical data in Entity Framework Code First

ぐ巨炮叔叔 提交于 2019-12-08 02:36:22
问题 I have a hierarchical relationship defined for one of my tables where the relationship is stored in a separate join table. The join table also contains information about the type of relationship. The (simplified) schema looks like: Bills ID int IDENTITY(1,1) NOT NULL (PK) Code varchar(5) NOT NULL Number varchar(5) NOT NULL ... BillRelations ID int IDENTITY(1,1) NOT NULL (PK) BillID int NOT NULL RelatedBillID int NOT NULL Relationship int NOT NULL ... I have FK relationships defined on BillID

Pluggable Conventions in Entity Framework

♀尐吖头ヾ 提交于 2019-12-08 01:46:39
问题 I am following EF Feature CTP5: Pluggable Conventions to create custom pluggable convention (in this particular case to change the precision of all decimal fields). Looks like in the latest release of EF the Add method on ConventionsConfiguraions is also "internal'. How do i add custom Pluggable Conventions now? 回答1: The feature has been removed in EF 4.1 and a possible implementation postponed to a later release: Code First customizable (pluggable) conventions are not supported. Removing the

Why lazy loading does not work in razor views (cshtml files)?

一曲冷凌霜 提交于 2019-12-08 01:39:23
问题 I write following statements in my cshtml file:- @{ string categoryName = string.Format("{0}->{1}", label.Category.Parent.Name, label.Category.Name); @categoryName } and get an exception Object reference null . On the other hand the same works in the Controller.cs file and in the immediate window too. I know it is related to lazy loading. Is there any concept behind it, that it does not work in Expressions in Cshtml files ? Thanks 回答1: This doesn't work because your context is disposed by the

Client Side validation in MVC 3 Database First

泄露秘密 提交于 2019-12-08 01:36:40
问题 If I add [Required] in my entity class then unobtrusive validation works fine. [Required] is not added where I generate my entity class using database first(*.edmx). If I manually add [Required] in my entity property, something like [Required] public int {get;set;} [Required] will delete when I update my edmx. So my question is how can I perform client side validation if I use database first in EF. 回答1: Create a partial class for your entity and use the MetadataType attribute. See example

Entity Framework RC1 DbContext query issue

邮差的信 提交于 2019-12-07 21:08:06
问题 I'm trying to implement the repository pattern using entity framework code first rc 1. The problem I am running into is with creating the DbContext. I have an ioc container resolving the IRepository and it has a contextprovider which just news up a new DbContext with a connection string in a windsor.config file. With linq2sql this part was no problem but EF seems to be choking. I'll describe the problem below with an example. I've pulled out the code to simplify things a bit so that is why

Entity Framework Model with Table-per-Type Inheritance

懵懂的女人 提交于 2019-12-07 17:51:55
问题 When I define a model in Entity Framework with Table-per-Type Inheritance, if I have a base class/table (not abstract) called person, and two sub entities and tables, adult and child, after creating a child, how would I take the same object and convert it to adult? Once converted to adult, the child record should be deleted, though the base class data in the person table should be retained. 回答1: It is not possible. It is similar problem like here. Simply the entity exists and its type is

Mapping to a nested class

早过忘川 提交于 2019-12-07 14:42:25
问题 I am getting the following error when my application runs: System.InvalidOperationException: The type 'ContactModels+Contact' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject. It is failing when my DBContext class tries to initialize the entities: public class DB : DbContext { public DbSet