Disabling Column in Entity Framework Code First - c#

I'm using Entity Framework to insert data into 2 different databases. There are a few columns that are present in one of the databases but not the other. Their data types are not nullable (int and float).
I don't use these columns (when they are present) in my code. Meaning I only insert 0 as the data for them but I can't send null obviously.
Is there a way for me to insert data with ease without creating 2 different versions of my app for these? Ideally I'd like to just have one model with something like an attribute that says insert 0 in this column if it's available.

If your application runs only against one database, then you can just use an IF statement in your OnModelCreating that uses the Fluent API to .Ignore() the missing properties.
public class MyDbContextWithMissingColumns: MyDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (myConfig.UseDatabaseWithoutSomeProperties)
{
modelBuilder.Entity<Foo>().Ignore(f => f.SomeProperty);
}
base.OnModelCreating(modelBuilder);
}
}
If a single instance of your application connects to both databases, then you have to use separate DbContext subtype, as OnModelCreating only runs for the first instance of a DbContext type in an AppDomain.
EG:
public class MyDbContextWithMissingColumns: MyDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>().Ignore(f => f.SomeProperty);
base.OnModelCreating(modelBuilder);
}
}

In the repository for the database with the restricted fields create the entity:
public class MyClass
{
int MyCommonClassID {get; set;}
string Name {get; set;}
[NotMapped]
string PhoneNumber {get; set;}
}
Where the attribute [NotMapped]. is used that field will not appear in the database but you can use it everywhere else. That wat you determine what gets written at the lowest level and your application doesn't care.

Related

Create New Table EF Code First with Match existing class

I have class with the following struct:
public class student
{
public int Id {get;set;}
public string name {get;set;}
}
I use the following configuration on model creation to match table column with class in the database based on parameter for example I have ,
16_student,17_student,18_student..... all these tables match the student class
public class studentConfiguration : EntityTypeConfiguration<Models.student>
{
public studentConfiguration (string SchoolId)
{
this.ToTable(SchoolId + "_student");
}
}
The previous function work fine with existing Tables , but how i can force the EF to create new table based on new parameter
in the other word i need if i pass parameter not exist let's say schoolId = 55 new table should be created with the name 55_student if not exist
I Enable Automatic migration now and the tables created successfully when it was not exists but the problem now when I add new school with id 56 the previous tables deleted so i can only add one table each time
is there any where to prevent migration from call delete for these tables just create ??
If I understand correctly, you have a database table with a table that has a name that is composed from several values of variables. You also hava a DbSet<TEntity> in your DbContext, and you want to tell entity framework that this DbSet is modeled in your database in a table that has this composed name.
Seeing your advanced usage of class EntityTypeConfiguration I assume that you know that this is done in function DbContext.OnModelCreating
The raison d`ĂȘtre of DbContext is to connect your entity framework classes to the actual database that your classes use. The proper way to model your database would be in this class.
Apparently your override of 'DbContext.OnModelCreating' creates a StudentConfiguration object. This object will do the actual configuring of the Student entity.
You probably will do this for several entities of your database. All these configurations will need similar information. One of them is the name of the table, others could be the maximum length of certain strings, or the precision of decimals etc.
The proper way to do this, is to give your StudentConfiguration an object (o an interface) that contains this information. The whole idea is similar to the factory design pattern:
interface ISchoolRequirements
{
public string StudentTableName {get;}
... // other items that differ per school
}
class MySchoolRequirements : ISchooRequirements
{
... // properties needed to create a StudentTableName
// the function that composes the StudentTableName from the properties
private string CreateStudentTableName() {...}
// implementation of ISchoolRequirements
public string StudentTableName {get{return this.CreateStudentTableName(); }
}
public class studentConfiguration : EntityTypeConfiguration<Models.student>
{
public studentConfiguration (ISchoolRequirement schoolRequirements)
{
this.ToTable(schoolRequirements.StudentTableName);
... // other configuration items
}
}
Your DbContext with OnModelCreating:
class MyDbContext : DbContext
{
ISchoolRequirements SchoolRequirements {get; set;}
public DbSet<Student> Students {get; set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// if no school requirements set, use default school requirements:
ISchoolRequirements schoolRequirement = this.SchoolRequirements ??
CreateDefaultSchoolRequirements();
// create the configurations:
modelBuilder.Configurations.Add(new StudentConfiguration(schoolRequirements));
modelBuilder.Configurations.Add(new TeacherConfiguration(schoolRequirements));
modelBuilder.Configurations.Add(new ClassRoomConfigurations(schoolRequirments));
... // etc
}
}
The advantage is that the creator of your DbContext has full control on the naming of the tables, the maximum size of certain strings, the precision of decimals, the type of DateTime etc. If the user of your DbContext does not need this control, some default configuration is used. You could even decide to read this default configuration from a configuration file

Entity Framework Code First table naming issue, singular confilcts with Plural

I have 2 table which I'm trying to access in MVC, one called Employees and one called Accountable. This is my code: -
public class dbEntity: DbContext
{
public dbEntity(): base("name=dbEntity") {}
public DbSet<Accountable> Accountable { get; set; }
public DbSet<Employees> Employees { get; set; }
}
The problem is the code complains that it can't find the table 'Accountables', I know I can add this line: -
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); }
But then the code complains that it can't find 'Employee'. At the moment it is not practical to rename the tables, is there another way around it?
Thanks
Add a data annotation of your table's name in the database to your context class.
[Table("TableName")]

Entity Framework Code First - How to ignore a column when saving

I have a class called Client mapped to a database table using Entity Framework code first. The table has a computed field that I need available in my Client class, but I understand that it won't be possible to write to this field. Is there a way of configuring Entity Framework to ignore the property when saving, but include the property when reading?
I have tried using the Ignore method in my configuration class, or using the [NotMapped] attribute, but these prevent the property from being read from the database.
You can use DatabaseGeneratedAttribute with DatabaseGeneratedOption.Computed option:
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public ComputedPropertyType ComputedProperty { get; set; }
or if you prefer fluent api you can use HasDatabaseGeneratedOption method in your DbContext class:
public class EntitiesContext : DbContext
{
public DbSet<EntityType> Enities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityType>().Property(e => e.ComputedProperty).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
Mark property as computed:
modelBuilder
.Entity<MyEntityType>()
.Property(_ => _.MyProperty)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

EF Code First table naming issues

I have a project built using EF code first. It also uses forms authentication. Until recently the membership database and the application database were being developed separately, but I want to combine them into one database for simplicity.
One of the classes in my code first model is called "Application" so the EF-generated table is called "Applications" which conflicts with a membership table of the same name. This is an example of my current context:
public partial class ExampleContext : DbContext
{
public DbSet<Application> Applications { get; set; }
public DbSet<Status> StatusTypes { get; set; } // notice the name of the property vs the name of the class
}
I thought the table names were based on the names of the properties in the context, because it was generating a table named StatusTypes for all of the Status objects. But if I rename the Applications property to something like MyApplications it is still generating a table named Applications. So clearly it's not just the name of the property and I'm missing something.
My question: how do I get EF to name this table differently?
Couldn't you use the configuration class to do something like this:
public class ApplicationConfiguration : EntityTypeConfiguration<Application>
{
public ClientConfiguration()
{
ToTable("SpecialApplication");
}
}
Then in your context override OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ApplicationConfiguration());
}
This should then force your table to be named SpecialApplication and avoid the conflict
By default, Entity framework code first will generate pluralized names for tables when it builds the db from the model classes. You can override the OnModelCreating method of your db context class specify a different name for the table.
public class YourDBCOntext:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Applications>().ToTable("MYApplications");
}
}
You can do this globally also so that none of the tables will have pluralized names.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTaleNameConvention>();
}
}
I just renamed the class and it worked. Easiest solution for now.

Entity Framework throws exception - Invalid object name 'dbo.BaseCs'

I've followed Adam's answer here and the Entity Framework now works and the Seed() method also works.
But when I try to access the database like this:
public User FindUserByID(int id)
{
return (from item in this.Users
where item.ID == id
select item).SingleOrDefault();
}
.............................................................................
// GET: /Main/
public ActionResult Index(int? id)
{
var db = UserDataBaseDB.Create();
if (!id.HasValue)
id = 0;
return View(db.FindUserByID(id.Value));
}
It throws an exception at return (from item in this.Users stating:
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'.
I've tried replacing it with:
return this.Users.ElementAt(id); but then it throws this exception.
LINQ to Entities does not recognize the method 'MySiteCreator.Models.User ElementAt[User](System.Linq.IQueryable1[MySiteCreator.Models.User], Int32)' method, and this method cannot be translated into a store expression.`
Can anyone help me?
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'
This error means that EF is translating your LINQ into a sql statement that uses an object (most likely a table) named dbo.BaseCs, which does not exist in the database.
Check your database and verify whether that table exists, or that you should be using a different table name. Also, if you could post a link to the tutorial you are following, it would help to follow along with what you are doing.
It is most likely a mismatch between the model class name and the table name as mentioned by 'adrift'. Make these the same or use the example below for when you want to keep the model class name different from the table name (that I did for OAuthMembership). Note that the model class name is OAuthMembership whereas the table name is webpages_OAuthMembership.
Either provide a table attribute to the Model:
[Table("webpages_OAuthMembership")]
public class OAuthMembership
OR provide the mapping by overriding DBContext OnModelCreating:
class webpages_OAuthMembershipEntities : DbContext
{
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
var config = modelBuilder.Entity<OAuthMembership>();
config.ToTable( "webpages_OAuthMembership" );
}
public DbSet<OAuthMembership> OAuthMemberships { get; set; }
}
If you are providing mappings like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ClassificationMap());
modelBuilder.Configurations.Add(new CompanyMap());
modelBuilder.Configurations.Add(new GroupMap());
....
}
Remember to add the map for BaseCs.
You won't get a compile error if it is missing. But you will get a runtime error when you use the entity.
It might be an issue about pluralizing of table names. You can turn off this convention using the snippet below.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
If everything is fine with your ConnectionString check your DbSet collection name in you db context file. If that and database table names aren't matching you will also get this error.
So, for example, Categories, Products
public class ProductContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
should match with actual database table names:
EF is looking for a table named dbo.BaseCs. Might be an entity name pluralizing issue. Check out this link.
EDIT: Updated link.
My fix was as simple as making sure the correct connection string was in ALL appsettings.json files, not just the default one.
Instead of
modelBuilder.Entity<BaseCs>().ToTable("dbo.BaseCs");
Try:
modelBuilder.Entity<BaseCs>().ToTable("BaseCs");
even if your table name is dbo.BaseCs
For what it is worth, I wanted to mention that in my case, the problem was coming from an AFTER INSERT Trigger!
These are not super visible so you might be searching for a while!
I don't know if is the case,
If you create a migration before adding a DbSet your sql table will have a name of your model, generally in singular form or by convention we name DbSet using plural form.
So try to verifiy if your DbSet name have a same name as your Table. If not try to alter configuration.
Most probably the translated SQL statement can't find the table name.
In my case, the table was assigned to a different schema. So, in the model you should enter the schema definition for the table like this:
[Table("TableName", Schema = "SchemaName")]
public class TableName
{
}
You have to define both the schema and the table in two different places.
the context defines the schema
public class BContext : DbContext
{
public BContext(DbContextOptions<BContext> options) : base(options)
{
}
public DbSet<PriorityOverride> PriorityOverrides { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasDefaultSchema("My.Schema");
builder.ApplyConfiguration(new OverrideConfiguration());
}
}
and for each table
class PriorityOverrideConfiguration : IEntityTypeConfiguration<PriorityOverride>
{
public void Configure(EntityTypeBuilder<PriorityOverride> builder)
{
builder.ToTable("PriorityOverrides");
...
}
}
In EF (Core) configuration (both data annotations and fluent API), the table name is separated from the schema.
Remove the "dbo." from the table name and use the ToTable overload with name and schema arguments:
.ToTable("MyUsers", "dbo");
Or taking into account that dbo is the default schema (if not configured differently), simply:
.ToTable("MyUsers");
As it is currently, it considers table dbo.dbo.MyUsers which of course does not exist.
The solution is very simple.
Just run the migration. Make sure you have the migrations folder with the code. Then on the Configure method of startup, put this code first in you method body:
using (IServiceScope scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetService<FRContext>().Database.Migrate();
}
This update the database, but its base in the migrations folder. I created the database, but if it does not find the migration folder with the files, it will create the database without tables, and you app will break at runtime.
Use table annotation in your class and use the name same as the name of view or table in the database
[Table("tbl_Sample")]
public class tblSample
{
//.........
}
In the context definition, define only two DbSet contexts per context class.
Wrong DB configuration in my tests while I was looking at the configuration from the project being tested.
In my case, I put the wrong ConnectionString name in connectionString Configuration in Startup.cs, Your connectionstring name in startup.cs and appsettings.json must be the same.
In appsettings.json:
In startup.cs:
Thats why when i made query using this context, it found no connectionstring from the appsettings when there was a wrong name. Consequently, it results Invalid Object Name 'a db table name'.

Categories

Resources