How to migrate `bool` data type to `Yes/No` field? - entity-framework-core

I'm using EntityFrameworkCore.Jet and I have a model class with a bool data type:
public class MyTestClass
{
...
public bool BoolField {get;set;}
...
}
When creating and applying a migration involving this class the target BoolField field in the target MS Access MyTestclass table is created as having Number (Integer) data type. How to make it created as a Yes/No (Bool) MS Access data type ?

Related

ADO.Net - Map table char column to enum

In an existing SQL table a nvarchar column holds enumerative informations.
I now try to use a enum class to map this column using ADO.Net Entity Framework. But somehow this does not seems possible since it requires an integer column.
Does ADO EF not support enums mapped to char columns, or how to realize it allowing linq or lambda syntax on querying?
Error: Given element assignment is invalid.
I am more used to hibernate orm where this is easily possible.
You can use private properties in your model to map your data to whatever property type you want.
// Model
public class Piece
{
// Subclass Piece to add mappings for private properties
public class PieceConfig : EntityTypeConfiguration<Piece>
{
public PieceConfig()
{
Property(b => b.dbtype); // needed for EF to see the private property
}
}
[Column("type", TypeName = "VARCHAR")]
private string dbtype { get; set; }
[NotMapped]
public PIECE type
{
get { return (PIECE)Enum.Parse(typeof(PIECE), dbtype); }
set { dbtype= value.ToString(); }
}
}
Then you just need to add the configuration to your OnModelCreating method
modelBuilder.Configurations.Add(new Piece.PieceConfig());

MVC4 and Entity Framework Inheritance and Include

I have some simple objects
public class DataClass
{
public int id;
public string Data;
}
public class Job()
{
public int id;
}
public class NewJob : Job
{
public DateTime StartDate;
public DataClass data;
}
I have then defined them in my dBContext()
public DbSet<Job> Jobs { get; set; }
public DbSet<DataClass> DataClass { get; set; }
Now if I use the following code
NewJob job = (NewJob) db.Jobs.Find(id);
This works fine but returns "data" as null
I know I define the class with the virtual keyword and it works and populates the "data" object.
public class NewJob : Job
{
public DateTime StartDate;
public virtual DataClass data;
}
But in my case I "normally" do not want the "data" object to be populated. So I need to load it on demand.
If I try something like
NewJob job = (NewJob)db.Jobs.Include("data").First();
I get an exception
A specified Include path is not valid. The EntityType 'Models.Job' does not declare a navigation property with the name 'data'.
I guess this is because it is looking at "job" and not "NewJob" when it is trying to do the include.
I also do not like the include with a string - no design time checking.
It looks like you are trying to convert data object to your domain object via type casting which is a very bad idea. What you want to do is grab your data object, instantiate your domain object, and map your data values to the domain object using some type of helper class. A very helpful tool I have been using is Automapper. Its a tool that will allow you to map one object to another. It also allows the use of regular expression to help with the mappings if the naming conventions between the 2 objects are different.
If you're using Entity Framework Code First and want to create instances of derived classes/entities you should do the following:
using (var db = new MyDbContext())
{
var newJob = db.Jobs.Create<NewJob>();
newJob.data.Data = "some data for a new job"; // this is string Data from DataClass
db.Jobs.Add(newJob);
db.SaveChanges();
}
After a lot of searching I found the following which can help.
If you include the System.Data.Entity namespace in your using clause then you can use the extension method .Include() after OfType<>() which is not normally available.
Slightly different code sample
using System.Data.Entity;
NewJob job = (NewJob)db.Jobs.OfType<NewJob>().Include(m => m.data).Where(x => x.id == id).FirstOrDefault();
This seems to be working for me in the example I used.

entityframework Inherited classes in single table can not use timestamp?

I want to create a TimeStamp field in Inherited class like this:
[Table("TABLE_A")]
public class A
{
public int ID {get;set;}
public string Name {get;set;}
}
[Table("TABLE_B")]
public class B : A
{
public string Address {get;set;}
[TimeStamp]
public byte[] RowVersion {get;set;}
}
but failed, how can I do here ?
You will see error
Type 'B' defines new concurrency requirements that are not allowed for
subtypes of base EntitySet types.
That means exactly what error says. Entity Framework do not support concurrency checks in derived types. You will see same error if you'll add simple concurrency check instead of timestamp:
[Table("TABLE_B")]
public class B : A
{
[ConcurrencyCheck]
public string Address { get; set; }
}
If you will move concurrency checking to base class, then it will work, but only on base type. If you need checking to be performed on derived type, I think you should use Stored Procedure for updating entity.

DbContext.Set<TEntity> Returns Derived Class Type

I am using entity framework code first, I have 2 entities declared like so:
[Table("BaseTable")]
public class BaseEntity
{
public string SomeProperty{get; set;}
}
[Table("DerivedTable")]
public class DerivedEntity
: BaseEntity
{
public string SomeOtherProperty {get; set;}
}
The problem is when I call the generic set method on the dbcontext it always returns the entities as DerivedEntity types. (EF is setting up the correct table mapping in the database just not returing the expected entity type from the set() method)
DbContext.Set<BaseEntity>();
How can I force Set<BaseEntity>(); to return the entities as type BaseEntity so I can only update that table?
You cannot. EF works on entity level not on table level. So if your entity is of type DerivedEntity it will never be loaded as BaseEntity only. When you modify attached entity EF will build update command only for modified columns so it should not modify your second table if you are only changing property from BaseEntity table.

Entity framework code first creates "discriminator" column

I am using EF CF approach for a website with MySQL.
For some reason EF creates a column in my Post table called "Discriminator" and contains the VARCHAR "Post".
Why is this column created? Can I do something to avoid it being created? Are there any advantages of having this column?
The Discriminator column is used and required in Table-Per-Hierarchy inheritance scenarios. If you for example have a model like this ...
public abstract class BaseEntity
{
public int Id { get; set; }
//...
}
public class Post : BaseEntity
{
//...
}
public class OtherEntity : BaseEntity
{
//...
}
... and make the BaseEntity part of the model, for instance by adding a DbSet<BaseEntity> to your derived context, Entity Framework will map this class hierarchy by default into a single table, but introduce a special column - the Discriminator - to distinguish between the different types (Post or OtherEntity) stored in this table. This column gets populated with the name of the type (again Post or OtherEntity).
You can stop the column being created by adding the [NotMapped] data annotation to the models that are inheriting from your base class. This will tell EF not to add your class to future migrations, removing the discriminator column.
public class BaseClass
{
}
[NotMapped]
public class InheritingClass : BaseClass
{
}
For completeness, if you want to use the fluent API to stop the inheriting class from being mapped with entity (and therefore stopping the discriminator column being created) you can do the following:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<InheritingClass>();
}