duplicity records: Problems with entity framework and sql views

1.- Modifying context model public string DbSet(EntitySet entitySet) { //you can modify to context model if all your view start with the same name. if(entitySet.Name.StartsWith("VW_")) { return string.Format( CultureInfo.InvariantCulture, "{0} System.Data.Entity.Infrastructure.DbQuery<{1}> {2} {{ get {{return this.Set<{1}>().AsNoTracking();}} }}", Accessibility.ForReadOnlyProperty(entitySet), _typeMapper.GetTypeName(entitySet.ElementType), _code.Escape(entitySet)); } else { return string.Format( CultureInfo.InvariantCulture, "{0} DbSet<{1}> {2} {{ get; set; }}", Accessibility.ForReadOnlyProperty(entitySet), _typeMapper.GetTypeName(entitySet.ElementType), _code.Escape(entitySet)); } } 2.- Add .AsNoTracking() with linq ModelEntities db = new ModelEntities db.SomeView.AsNoTracking();
I had a problem with some query when i tried to get records from sql view in my entity model, all records was duplicate...

reading in others post i found the problem is that all SQL view has not primary key, and entity framework acts some weird when mapping the SQLs views, because them doesn't have primary keys.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.