Entity Framework 6.0 - Evaluation of method System.Linq.Queryable.Whewre() requires use of the static field - entity-framework

I am using Entity Framework 6.0 with .NET framework 4.5 inside a SharePoint 2013 project, to get data from a view in a database based on a specific condition.
My Context.cs file looks like this:
namespace Clients
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class Clients_ReqsEntities : DbContext
{
public Clients_ReqsEntities()
: base("name=Clients_ReqsEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ClientInfo> ClientInfo { get; set; }
}
}
And my ClientInfo.cs file looks like this:
namespace Clients
{
using System;
using System.Collections.Generic;
public partial class ClientInfo
{
public string ClientTitle { get; set; }
public string ClientID { get; set; }
public string ClientUNumber { get; set; }
}
}
I am trying to get data from this view based on the ClientUNumber, using this query:
string anyValue = "something";
var client = (context.ClientInfo.Where(c => c.ClientUNumber.Trim() == anyValue)).FirstOrDefault();
The returned value is always "null", however if I run that line of code in Watch window, I am getting this error:
Evaluation of method System.Linq.Queryable.Where() requires use of the static field System.Data.Entity.Core.Objects.ObjectQuery`1[Clients.ClientInfo].MergeAsMethod, which is not available in this context.
I tried using this query too:
(from c in context.ClientInfo where c.ClientUNumber.Trim() == anyValue select c).FirstOrDefault();
But I am getting the same error.
Some posts suggest to use ToList(), but this will get all records from the DB, and I keep getting Evaluation Timedout. So I don't think this is the right approach. PS: the view has 7k records.
Any help is appreciated.

Related

Swagger-net breaks when using [FromUri] with a complex EF model

I'm using Swagger-Net in my .NET 4.5.1 WebAPI project and one of my API calls is causing the Swagger UI to spin forever on load before coming back with the error below.
Specifically, I found that using [FromUri] in combination with a complex EF entity that has references to other entities ends up causing this.
[HttpPost]
public APIResponse CreateSchool([FromUri]School school)
{
// save school object to db
}
public partial class School : IAuditableEntity,IEntity
{
public School()
{
this.Affiliations = new HashSet<Affiliation>();
this.SchoolAccreditations = new HashSet<SchoolAccreditation>();
this.SchoolAdultRoles = new HashSet<SchoolAdultRole>();
this.SchoolCareOptions = new HashSet<SchoolCareOption>();
this.SchoolDailySessions = new HashSet<SchoolDailySession>();
this.SchoolEligibilityRequirements = new HashSet<SchoolEligibilityRequirement>();
// ...more hashsets
[DataMember]
public int SchoolID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public bool Active { get; set; }
//...more properties
}
}
Is there a way to still use FromUri and the EF model? Or do I need to change my API call signature?

Invalid Column name error in MVC 4

I have created a simple class and a list based on this class. When i try to populte this list and send to view iam getting an error. Please view my class and custom mapper model based on database.
Folloiwng is the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcModal.Models
{
public class mytransaction
{
public int Id { get; set; }
public int my_trn_id { get; set; }
public string Description { get; set; }
public List<mytransaction> Translist { get; set; }
}
}
Following is the custom database mapper class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using MvcModal.Models;
namespace MvcModal.Models
{
public class PrContext : DbContext
{
static string _conString = #"Data Source=.\sqlexpress;Initial Catalog=MyDb;Integrated Security=True";
public PrContext() : base(_conString) { }
public DbSet<mytransaction> MyTransactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<PrContext>(null);
modelBuilder.Configurations.Add(new NewTransactMapper());
}
public class NewTransactMapper : EntityTypeConfiguration<mytransaction>
{
public NewTransactMapper()
{
this.ToTable("mytransaction");
this.Property(m => m.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasKey(m => m.my_trn_id);
}
}
}
}
Following is the error image.
Please view the red circuled error and see the mytransactions_my_trn_id text. mytransaction is my table name and my_trn_id is my column name. Rest of the columns have no issue, but this making me insane.
Please anyone guide what iam missing and how can i make my table name and column isolate and resolve this error. Thanks in advance.
If you want to create the model you have to know wich columns EF generates to handle relationships and if you don't specify the names (as in this case) you have to know wich name EF will assign to properties.
I suggest you (I do this) to generate the model on an empty database with EF standard migrations then copy the structure from the EF created database.
In your case you only need to add the column mytransaction_my_trn_id of type int (same as id). If you need the same database the EF would generate with migrations, you need also to add an index on that column and a relationship from that column to my_trn_id column (primary key).
I have done it using the following code. It may also be help someone. Also thanks to everyone for their nice suggestions.
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using payorder_draft_printing.Controllers;
namespace payorder_draft_printing.Models
{
public class context_db : DbContext
{
static string _conString = #"Data Source=my datasource";
public context_db()
: base(_conString)
{
Database.SetInitializer<context_db>(null);
}
public IDbSet<sms_description> sms_description { get; set; }
public IDbSet<sms_imported_trn_code> sms_imported_trn_code { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new sms_description_mapper());
modelBuilder.Configurations.Add(new sms_imported_trn_code_mapper());
}
}
class sms_description_mapper : EntityTypeConfiguration<sms_description>
{
public sms_description_mapper()
{
ToTable("dbo.sms_description");
this.Property(x => x.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasKey(x => x.trn_code);
Property(x => x.trn_code).HasColumnName("trn_code").IsRequired();
}
}
class sms_imported_trn_code_mapper : EntityTypeConfiguration<sms_imported_trn_code>
{
public sms_imported_trn_code_mapper()
{
ToTable("dbo.sms_imported_trn_code");
HasKey(x => x.trn_code);
this.Property(x => x.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.trn_code).HasColumnName("trn_code").IsRequired();
}
}
}

Database First Validation

I have an auto-generated Entity Framework model. It was generated using a database first approach. The mid_initial column has a database defined constraint that limits the column to a maximum length of 3 characters.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Agency.DataAccess.RegistrationModel
{
using System;
using System.Collections.Generic;
public partial class Registrant
{
public Registrant()
{
}
public int id { get; set; }
public string fname { get; set; }
public string mid_initial { get; set; }
public string lname { get; set; }
}
}
When I try and create a model with a mid_initial greater than 3 characters, a invalid state, ModelState.IsValid is returning true. Because of this db.SaveChanges is then called, which then raises DbEntityValidationException.
[HttpPost]
public ActionResult Create(Registrant registrant)
{
try
{
if (ModelState.IsValid)
{
Debug.WriteLine("Entity was valid.");
db.Registrants.Add(registrant);
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Create", registrant);
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
return View(registrant);
}
}
Why is the ModelState.IsValid method returning true? It would seem that my model is not aware of the maximum length constraint. How do I make it aware?
EF db-first can't infer constraints from database.
Use the MaxLenght data annotation attribute:
public partial class Registrant
{
public Registrant()
{
}
public int id { get; set; }
public string fname { get; set; }
[MaxLength(3, ErrorMessage = "")]
public string mid_initial { get; set; }
public string lname { get; set; }
}
Note: this class is a auto generated class and every time you update and save your model (.EDMX file), this code will be overwritten and you'll loose your attributes.
To avoid that, you should extend your classes with some partial classes with the same name and same namespace as your auto-generated classes. If you need examples to show you how, tell me to put it in answer.
MVC is EF-agnostic, and as such doesn't implicitly attempt to validate the model using EF validation to populate its ModelState.
You have four basic solutions I can think of right now:
Hook them up yourself, for example using MVC filters, DbContext.GetValidationErrors and ModelState.
Find and use third-party code that does this already.
Validate the code separately using facilities that MVC can use, for example using DataAnnotations. You may try to generate them automatically by modifying the EF T4 template. Note that this is still technically redundant (the code will be validated twice, once by MVC, once by EF).
Submit a patch for MVC so that it can support EF explicitly (as a soft dependency) and make it all just work (both projects are open source) -- or downvote me because they already did so and I never knew it.

Error comes as a {"Invalid object name 'dbo.TableName'."}

I'm using Entity Framework and MVC3,
I have used Model First approch...
I have used Company as a Base class and I have inherited the Lead Class from it.
When I run the application its gives an error...
This is Base Class
using System;
using System.Collections.Generic;
namespace CRMEntities
{
public partial class Company
{
public int Id { get; set; }
}
}
This is Lead Class (Child)
using System;
using System.Collections.Generic;
namespace CRMEntities
{
public partial class Lead : Company
{
public Lead()
{
this.Status = 1;
this.IsQualified = false;
}
public Nullable<short> Status { get; set; }
public Nullable<bool> IsQualified { get; set; }
}
}
I have added the controller,and in index view I have added this code...
public class Default1Controller : Controller
{
private CRMWebContainer db = new CRMWebContainer();
//
// GET: /Default1/
public ViewResult Index()
{
return View(db.Companies.OfType<Lead>().ToList());
}
}
This is DB and Model ...
Its giving the inner error -
{"An error occurred while executing the command definition. See the
inner exception for details."} {"Invalid object name
'dbo.Companies'."}
Do you have a Companies table or Company table in your database. It looks like you have a Mapping issue. Entity Framework will make some guesses as to how it pluralizes entity names by default.

seed method not called with EntityFramework CodeFirst

I've been struggling on and off with this problem since 4.1 (now I'm on 4.3). It seems to me that to get the seed method called, all I should have to do is the following:
1) Create an empty data catalog on sqlserver
2) Execute the code below:
Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>());
I have my SiteDB defined as follows:
public class SiteDBInitializer :
DropCreateDatabaseAlways<SiteDB>
{
protected override void Seed(SiteDB db)
{
... (break point set here that never gets hit)
I feel like I must be missing something very simple because this creates my tables, but does never calls the seed method.
To Make this more clear, here is a full example that includes all the code. When I run it, seed never gets called:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace ConApp
{
internal class Program
{
private static void Main(string[] args)
{
Database.SetInitializer(new SiteDBInitializer());
using (var db = new SiteDB())
{
var x = db.Customers;
}
}
}
public class SiteDB : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string LastName { get; set; }
}
public class SiteDBInitializer :
DropCreateDatabaseAlways<SiteDB>
{
protected override void Seed(SiteDB db)
{
db.Customers.Add(new Customer() {LastName = "Kellner"});
db.Customers.Add(new Customer() {LastName = "Jones"});
db.Customers.Add(new Customer() {LastName = "Smith"});
db.SaveChanges();
}
}
}
You need call Database.SetInitializer(new SiteDBInitializer()); instead.
I looked at all the answers for that, nothing really works, and I wonder if that's a Microsoft bug for not calling the Seed method when DB does not exists.
The only code that worked, was to actually make the class call the seed if DB does not exists:
Context class:
class AlisDbContext : DbContext
{
public class MyContextFactory : IDbContextFactory<AlisDbContext>
{
public AlisDbContext Create()
{
return new AlisDbContext("CompactDBContext");
}
}
public AlisDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
Database.SetInitializer(new AlisDbInitializer(this));
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AlisDbContext>());
}
public DbSet<SavedCredentials> SavedCredentialses { get; set; }
}
Then AlisDbInitializer need to check and call the seed method like:
public AlisDbInitializer(AlisDbContext alisDbContext)
{
if (!alisDbContext.Database.Exists())
{
Seed(alisDbContext);
}
}