entity-framework-4.1

Entity Framework 4.1 - EFTracingProvider

白昼怎懂夜的黑 提交于 2019-11-27 00:24:26
问题 Is there a way to make the EFTracing provider work with EF 4.1? EFTracing seems to need an objectcontext and I use dbcontext. Thanks in advance! 回答1: Yes, you can. I'm using the community version with both database-first DbContexts and code-first DbContexts. This answer is based on a discussion thread on the project site. For database-first/designer DbContexts (via ADO.NET DbContext Generator templates), you can simply add the following constructor: public abstract class MyDbContext :

Can a DbContext enforce a filter policy?

痴心易碎 提交于 2019-11-27 00:06:18
I would like to pass a value to the ctor of a DbContext and then have that value enforce "filtering" on the related DbSets. Is this possible...or is there a better approach? Code might look like this: class Contact { int ContactId { get; set; } int CompanyId { get; set; } string Name { get; set; } } class ContactContext : DbContext { public ContactContext(int companyId) {...} public DbSet<Contact> Contacts { get; set; } } using (var cc = new ContactContext(123)) { // Would only return contacts where CompanyId = 123 var all = (from i in cc.Contacts select i); // Would automatically set the

Setting a foreign key to null when using entity framework code first

半世苍凉 提交于 2019-11-26 22:47:38
问题 I'm using the database first implementation of Entity Framework Code First as the data layer for a project, but I've run into a problem. I need to be able to set a foreign key to null in order to remove an association in the database. I have 2 objects. One is called Project. public class Project { public int ProjectId {get; set;} public Employee Employee {get;set;} } public class Employee { public int EmployeeId {get; set;} public string EmployeeName {get;set;} } This matches what I have in

Entity Framework Validation confusion - maximum string length of '128'

好久不见. 提交于 2019-11-26 22:21:10
问题 I'm faced with a confusing problem where in my Edit or Create action result methods, EF4 will throw a DbEntityValidationException with the inner message stating: The field Body must be a string or array type with a maximum length of '128'. The model in question looks like this: [Table("tblArticles")] public class Article { [Key] public int ID { get; set; } [Required(ErrorMessage="Title must be included")] public string Title { get; set; } [AllowHtml] public string Body { get; set; } [Required

error when using interfaces for Entity Framework (4.2) entities

我的梦境 提交于 2019-11-26 22:02:50
问题 I am using the the latest version of Entity Framework (4.2) and trying to implement interfaces for my Entities and for some reason, it isn't compiling. it is throwing an error " Cannot convert expression type ICollection<IOrder> to return type ICollection<Order> ". if I don't use interfaces for the entities, then I don't get this error. I have a separate project for interfaces (for repositories and services etc) and I need to pass the EF entities in those methods as parameters and I don't

Entity framework code-first null foreign key

你离开我真会死。 提交于 2019-11-26 21:57:38
I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public class User{ public int CountryId { get; set; } public Country Country { get; set; } } public class Country{ public List<User> Users {get; set;} public int CountryId {get; set;} } Error: A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = Country_Users ]"} You must make your

Generic Way to Check If Entity Exists In Entity Framework?

≯℡__Kan透↙ 提交于 2019-11-26 21:55:12
Similar to Best way to check if object exists in Entity Framework? I'm looking for a generic way to check for an entity in a DbSet . Something like this, which doesn't work: private DbContext DbContext { get; set; } private DbSet<T> DbSet { get; set; } public Boolean Exists(T entity) { return ((from item in this.DbSet where item == entity select item).Count() > 0); } The line where item == entity works in LINQ to SQL, but apparently not with LINQ to Entities. Since the entities may have different keys I can't have them all inherit from a common abstract with a known key for comparison. I could

Entity Framework v4.1 LIKE

风流意气都作罢 提交于 2019-11-26 21:49:52
问题 How do I have to build my query to result in an output SQL query like: SELECT [viewRegisters].[Id] AS [IdRegister] WHERE Name LIKE '%a%bc' OR SELECT [viewRegisters].[Id] AS [IdRegister] WHERE Name LIKE 'a%b%c' OR SELECT [viewRegisters].[Id] AS [IdRegister] WHERE Name LIKE 'a%b%c%' I'm using .Net Framework 4.0, Entity Framework v4.1 and C#. EF v4.1 converts this type of linq queries from: ((IQueryable<T>)Data).Where(z => z.Field.Contains("a%b%c%")); Into: SELECT [viewRegisters].[Id] AS [Id]

DbSet table name

不羁岁月 提交于 2019-11-26 21:31:08
问题 To get database table name on Entity framework 4.0 I do: ObjectSetInstance.EntitySet.ToString() Is there a way to do this on Entity Framework 4.1? 回答1: Try something like this: string name = (context as IObjectContextAdapter).ObjectContext.CreateObjectSet<MyClass>().EntitySet.Name; 回答2: Extension methods for DbContext and ObjectContext : public static class ContextExtensions { public static string GetTableName<T>(this DbContext context) where T : class { ObjectContext objectContext = (

Updating database schema with Entity Framework Code First

允我心安 提交于 2019-11-26 21:17:07
问题 Entity Framework Code First is a great framework for developing new projects. But what about extending an existing database? For instance what if I simply want to add an additional property to an existing data entity? Is there any "code first" way to do this or will I have to add a database column by hand using SQL Server Management Studio or a similar tool? All I have found so far is how to regenerate the entire database from scratch when the schema changes, but I don't want to lose my data.