Reputation: 7864
I'm using Visual Studio 2015, SQL Server 2014, EF 6, MVC 5. EF is throwing the following error for me on SaveChanges()
:
Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
I can't figure out what's wrong because I have no varbinary(max)
columns; I never have. Why is it trying to convert between the two? In case it matters, I tried switching the columns to varchar
but the same problem happens (with varchar in the error instead of nvarchar). How can I fix this problem?
I have migrations turned off by putting this in the MyContext
constructor:
Database.SetInitializer<NxContext>(null);
EF context:
public class MyContext : DbContext
{
public MyContext() : base("MyContext")
{
Database.SetInitializer<MyContext>(null);
}
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PersonMap());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Base class:
public class Person
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
EF mapping. I tried specifying the column types with HasColumnType("nvarchar")
but that had no effect. (That's the default for EF anyway, so it should be unnecessary.)
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
Property(u => u.Email).IsRequired().HasMaxLength(320);
Property(u => u.FirstName).IsRequired().HasMaxLength(75);
Property(u => u.LastName).IsRequired().HasMaxLength(75);
}
}
The simplified method that's updating the DB:
public class PersonService : IPersonService
{
public void Create()
{
using (var db = new MyContext())
{
var person = new Person
{
Email = "[email protected]",
FirstName = "TestFN",
LastName = "TestLN"
};
db.Persons.Add(person);
db.SaveChanges(); // error thrown here
}
}
}
DB schema:
Upvotes: 1
Views: 1236
Reputation: 28272
After some debate on the comments, it seems the problem was that the context was not getting the right connection string, and it was using a different database on (localdb)
, thus the strange errors.
Upvotes: 1