entity-framework

How to determine if Navigation Property in the Entity Framework is set without loading the related records

爱⌒轻易说出口 提交于 2020-01-21 04:23:26
问题 I'm not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation. Lets imagine this scenarios: A) I have two Entities A and B with a relation N to N (many to many) and tree Table in my Data Base A and B and a Link Table AB with two Foreign Key . In this scenario EF create a Navigational Property lets call it X and also a XReference . B) I have two Entities A and B with a relation 1 to N (one to many) and two Table in my Data Base A and B with one Foreign Key . In

Entity Framework - case insensitive Contains?

醉酒当歌 提交于 2020-01-21 03:55:08
问题 I want a solution to this problem that does not involve ToUpper or ToLower, as I use in the code below; var upper = term.ToUpper(); using (var db = this.DataContext) { return db.Counties.Where(x => x.CountyName.ToUpper().Contains(upper)).ToList(); } I am using entitly framework so the C# solution of using StringComparison.CurrentCultureIgnoreCase does not work. It does work for Equals , EndsWith and StartsWith , but not Contains . 回答1: I use EF6 and Sql Server and Contains is mapped to LIKE '

Entity Framework - case insensitive Contains?

落花浮王杯 提交于 2020-01-21 03:55:05
问题 I want a solution to this problem that does not involve ToUpper or ToLower, as I use in the code below; var upper = term.ToUpper(); using (var db = this.DataContext) { return db.Counties.Where(x => x.CountyName.ToUpper().Contains(upper)).ToList(); } I am using entitly framework so the C# solution of using StringComparison.CurrentCultureIgnoreCase does not work. It does work for Equals , EndsWith and StartsWith , but not Contains . 回答1: I use EF6 and Sql Server and Contains is mapped to LIKE '

EF code first - Model compatibility cannot be checked because the database does not contain model metadata

徘徊边缘 提交于 2020-01-21 00:49:27
问题 I have enabled automatic migrations. Then, I deleted my whole db. Next, i executed Update-database from command console, and it recreated my db. Then, I started my application only to see this error: Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations. So what exactly is that metadata, and how can I point entity framework to it? PS. My database

Working with nullable types in Expression Trees

被刻印的时光 ゝ 提交于 2020-01-20 16:51:48
问题 I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code: public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value) { ParameterExpression table = Expression.Parameter(typeof(T), ""); Expression column = Expression.PropertyOrField(table, columnName); Expression where = Expression.GreaterThanOrEqual(column, Expression.Constant

EF 4.1 / Linq-to-SQL: What is better: using Equals or ==?

与世无争的帅哥 提交于 2020-01-20 08:19:05
问题 We're having a discussion about using Equals or == on an int comparison in a LINQ query. We're using EF4.1 Code First. What is better? var query = context.Boodschappen .Where(b => b.BoodschapID == id).FirstOrDefault(); or: var query = context.Boodschappen .Where(b => b.BoodschapID.Equals(id)).FirstOrDefault(); And why ? 回答1: For Linq To Sql, you want neither. Instead, use Object.Equals: .Where(b => Object.Equals(b.BoodschapID, id) Why? Because of a Bug in the SQL Generator if id happens to be

How do I load related entities of type IEnumerable<T>

只愿长相守 提交于 2020-01-20 08:13:25
问题 I'm trying to load related entities of a SingleOrDefault entity, but I am getting the following exception: The navigation property of type IEnumerable is not a single implementation of type ICollection I've tried doing this several ways and ultimately get the same error above for each query against the context (I've included the other ways in comments). using(var context = CustomObjectContextCreator.Create()) { return context.Job.Include("Surveys").Include("SiteInfoes") .Where(r => r.Jobid ==

Virtual Navigation Properties and Multi-Tenancy

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-20 05:17:24
问题 I have a standard DbContext with code like the following: public DbSet<Interest> Interests { get; set; } public DbSet<User> Users { get; set; } I've recently implemented multi-tenancy by creating a TenantContext that contains the following: private readonly DbContext _dbContext; private readonly Tenant _tenant; public TenantContext(Tenant tenant) : base("name=DefaultConnection") { this._tenant = tenant; this._dbContext = new DbContext(); } public IQueryable<User> Users { get { return

Generating Interfaces from entity framework database first auto-generated code

一曲冷凌霜 提交于 2020-01-20 04:35:05
问题 I am using MVC3, C# 4.0 and Entity Framework in Visual Studio 2010. I am generating my edmx and Designed.cs files from a database. I am then generating interfaces from the entities in the Designer.cs file as part of my nLayer structure. The original code is public partial class DataEntrySummary : EntityObject which then becomes public partial class DataEntrySummary : EntityObject, Mb.Interface.IDataEntrySummary My concern is that when the database changes (and it will) and I regenerate the

Where to start with Entity Framework [closed]

偶尔善良 提交于 2020-01-19 23:32:34
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Anyone know a good book or post about how to start in EF? I have seen the DnrTV any other place? 回答1: Mike Taulty's Blog: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/category/1024.aspx A great EF intro deck: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/03/13