entity-framework-5

Entity Framework 5.0 composite foreign key to non primary key - is it possible?

烈酒焚心 提交于 2019-12-02 04:17:13
问题 I am using Entity Framework 5.0.0.0 in a .net 4.5 console application and I have to access a database with two tables in it with a foreign key relationship between them like so: The odd thing about it is that the foreign key is between B(Almost1, Almost2) and A(Almost1, Almost2) not from B(AId) to A(AId) . This is allowed by SQL server as Almost1 and Almost2 combined are unique and neither are nullable (on table A at least - on B they are as it is an optional relationship but that is by the

Filling child entity with Entity Framework SqlQuery

点点圈 提交于 2019-12-02 04:06:42
I have two entities in 1:n relationship: Category and Product. public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; } } public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public virtual Product { get; set; } } public class context : DbContext { public DbSet<Category> Categories { get; set; } public DbSet<Product> Products { get; set; } } Its possible to load products in every category by Eager loading. context.Categories.Include(c=>c.Products)

TextBoxFor() not generating validation markup

北城以北 提交于 2019-12-02 03:55:02
I have a SQL Server 2012 in which I have AWARD table with two columns TITLE and MONTH. TITLE is varchar(256) and cannot be NULL. MONTH is int and can be NULL. With VS2012 Ultimate and EF 5.0.0, the TextBoxFor helper in MVC4 app is not producing validation (data-val="required" and data-val-required="required message") for the TITLE columne above, but in the same View, MONTH is getting the correct validation markup. The .edmx designer does show TITLE is NonNullable, BUTT, the automatically generated AWARD.cs file does not have the [Required] attribute for the TITLE column. What can I try? @model

Get entity facets and other metadata on runtime

你说的曾经没有我的故事 提交于 2019-12-02 03:46:54
I have .NET 4.0 WinForms Application, and I use Entity Framework 5 with Model First Approach. In VS EF Designer, I have created a dozen or so entities with a lot of scalar properties of String type, then in Properties Toolbar I have configured parameters (i.e. General parameters, Facets Parameters) for them, to fit DB requirements. In BL layer I am able to validate entity object in purpose to check whether it contains correct values, for example by using DbContext.Entry(Of T)(entity).GetValidationResult() method. But I need to develop also GUI layer input fields validation for WinForms. I

How do I express a “has many through” relationship in Entity Framework 5?

こ雲淡風輕ζ 提交于 2019-12-02 01:36:58
I am attempting to use Entity Framework 5 to query an existing MySQL database. I used code-first to create a code-based model that maps to an existing database following this tutorial on MSDN . I have two tables: users and buddies . A User has an id , a name and an email . A Buddy has a user_id and a buddy_id . A User has many Buddies (which are also Users ). The buddy_id column is a foreign key back into the Users table. So each User has many Users through Buddies . public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public IList

Entity Framework 5.0 composite foreign key to non primary key - is it possible?

雨燕双飞 提交于 2019-12-02 01:32:41
I am using Entity Framework 5.0.0.0 in a .net 4.5 console application and I have to access a database with two tables in it with a foreign key relationship between them like so: The odd thing about it is that the foreign key is between B(Almost1, Almost2) and A(Almost1, Almost2) not from B(AId) to A(AId) . This is allowed by SQL server as Almost1 and Almost2 combined are unique and neither are nullable (on table A at least - on B they are as it is an optional relationship but that is by the by). Here's some SQL for creating this situation: CREATE TABLE [dbo].[A]( [AId] [int] IDENTITY(1,1) NOT

How to disable automapping of properties in Entity Framework

扶醉桌前 提交于 2019-12-02 01:12:56
问题 I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project. Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and

How to drop unique index with entity framework code first migrations

泪湿孤枕 提交于 2019-12-02 00:12:35
问题 I'm using Entity Framework 5.0 with Code First migrations enabled. I've added Unique key by using: CreateIndex("dbo.Groups", "Name", true); Now I want to remove existing Unique key with next migration's Down() method by using: DropIndex("dbo.Groups", "Name"); However I get the message: Cannot drop the index 'dbo.Groups.Name', because it does not exist or you do not have permission. I'm using connection string that assumes I'm DBO. What else could be wrong? 回答1: There is another answer to this

Entity framework DbContext update value without query and by foreign key

烈酒焚心 提交于 2019-12-01 21:37:49
问题 I have a method for updating some tables. For update I need get first of TestProcess , but I don't like that. How can I update TestProcess without select(firstOrDefault) operation, used only for the update operation? Example of method: public void UpdateTestProcess(int id, string updateID) { using (TestEntities context = new TestEntities()) { TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id); pr.UpdateID = updateID; context.TestProcess.Attach(pr); context

How to disable automapping of properties in Entity Framework

一个人想着一个人 提交于 2019-12-01 21:32:17
I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project. Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and property is mapped. Only way is to use Ignore method or NotMapped attribute, but I don't want to do it