Invalid Column name error in MVC 4 - entity-framework

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();
}
}
}

Related

Entity Framework Core 3 : Type must match overridden member

I have the following two dbcontexts in my Entity Framework Core solution. The OrganisationContext derives from SagitarriContext. I am overiding the base property DbSet<Person> Person. I am getting the following error in the derived class:
Error CS1715
'OrganisationContext.Person': type must be 'DbSet' to match overridden member 'SagitarriContext.Person'
DbContext
namespace Genistar.Data.DbContexts.Interfaces
{
public class SagitarriContext : DbContext, ISagitarriContext
{
public SagitarriContext();
public SagitarriContext(DbContextOptions<SagitarriContext> options);
protected SagitarriContext(DbContextOptions options);
public virtual DbSet<Person> Person { get; set; }
}
}
namespace Genistar.Data.DbContexts
{
public class OrganisationContext : SagitarriContext
{
private readonly ITimeProvider _timeProvider;
private readonly IUserContextResolverFactory _userContextResolver;
public OrganisationContext(DbContextOptions options)
: base(options)
{
}
public OrganisationContext(DbContextOptions options, ITimeProvider timeProvider, IUserContextResolverFactory userContextResolver)
: base(options)
{
_timeProvider = timeProvider;
_userContextResolver = userContextResolver;
}
public override DbSet<Person> Person { get; set; }
}
}
namespace Genistar.Data.DbContexts.Interfaces
{
public interface ISagitarriContext
{
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
DbSet<TQuery> Set<TQuery>() where TQuery : class;
public DatabaseFacade Database { get; }
DbSet<Person> Person { get; set; }
}
}
Usings
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Genistar.Data.DbContexts;
using Genistar.Data.Models;
using Genistar.Organisation.Models.Representative;
using Genistar.Organisation.Models.Unregistered;
using Genistar.Organisation.Models.User;
using Person = Genistar.Organisation.Models.DataModels.Person;
using PersonNote = Genistar.Organisation.Models.DataModels.PersonNote;
using Genistar.Security.Context;
using Genistar.Security.Utility;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
You are not in the same namespace, so the Person class used in DbSet<Person> might be a different one in Genistar.Data.DbContexts.Interfaces and Genistar.Data.DbContexts. Also, we don't see the usings, so there might be an error there.

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

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.

Adding WebApi to an existing MVC4 app that uses Entity framework

I've been going around with this for a few days now. I have an existing MVC 4 project that uses entity framework for database creation. The app works as intended but I have a new requirement to add web api to this site. This is a database of quotes and the requirement is to return a simple quote that only contains limited information of the full database entry.
My original model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Exercise4.Models
{
public class Quote
{
public int QuoteID { get; set; }
[Required (ErrorMessage = "A Category Field Must be selected or a New one must be Created before continuing")]
public int CategoryID { get; set; }
[Display (Name="Quotation")]
[Required (ErrorMessage = "A Quotation is Required")]
public string QName { get; set; }
[Display (Name= "Author")]
[Required (ErrorMessage = "An Authors Name is Required\n Use 'Unknown' if needed")]
public string QAuthor { get; set; }
[Display (Name = "Category")]
public virtual Category category { get; set; }
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime Date { get; set; }
public int UserId { get; set; }
}
}
The Simple Quote Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Exercise4.Models
{
public class SimpleQuote
{
public int Id { get; set; }
public string Quote { get; set; }
public string Author { get; set; }
public string Category { get; set; }
}
}
The Context (*Note the SimpleQuote entry was added automagicly when I scaffold the new QuoteApiController)
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Exercise4.Models
{
public class QuoteContext : DbContext
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<UserProfile> UserIds { get; set; }
public QuoteContext()
{
Configuration.ProxyCreationEnabled = false;
}
public DbSet<SimpleQuote> SimpleQuotes { get; set; }
}
}
This returns an error when accessing the /api/quoteapi/ page with
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Obviously this error occurs because it is trying to return a SimpleQuote Object that doesn't exist in the database.
The API Controller that was created.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Exercise4.Models;
namespace Exercise4.Controllers
{
public class QuoteApiController : ApiController
{
private QuoteContext db = new QuoteContext();
// GET api/QuoteApi
public IEnumerable<SimpleQuote> GetSimpleQuotes()
{
return db.SimpleQuotes.AsEnumerable();
}
// GET api/QuoteApi/5
public SimpleQuote GetSimpleQuote(int id)
{
SimpleQuote simplequote = db.SimpleQuotes.Find(id);
if (simplequote == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return simplequote;
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Where am I going awry. I can't return a model that doesn't exist in the database. If I change the call in the api to return a Quote model that works but I only need to return the quote, author and category as strings. Would I just return the Quote object in the controller, pull out the information I need and then return the SimpleQuote object? Not sure how to do that. Any suggestions?
You mentioned scaffolding, are you using Code First to create your Database?
Also you only want the SimpleQuote for returning the information, it looks like its added to your DB context as a table. When what you really want is to pull the data from the Quote Table and build or extract the information you want and return just that. If you don’t have to return a SimpleQuote Object and just return a string you could write something very simplistic like this.
public HttpResponseMessage GetSimpleQuote(int id)
{
var quote = db.Quotes.Find(id);
if (quote == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
var output += "ID: " + quote.Id + " Quote: " + quote.Quote + " Author: " + quote.Author + " Category: " + quote.Category ;
var response = new HttpResponseMessage();
response.Content = new StringContent(output);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return response;
}
I 1+ and Accepted #CHammond's response for putting me on the right track. I do need to return a SimpleQuote object. Here is the code I used.
public SimpleQuote GetSimpleQuote(int id)
{
var quote = db.Quotes.Find(id);
if (quote == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
SimpleQuote output = new SimpleQuote();
output.Quote = quote.QName;
output.Author = quote.QAuthor;
output.Category = db.Categories.Find(quote.CategoryID).CatName;
return output;
}

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);
}
}