Task this, when registering the customer in the database we need to create a set of tables with a unique prefix, it is generally possible to implement using EF?
I haven't tested this but it should work, or at least be a good start point. First you need to tell your context the prefix you will be using, I'd do this in the constructor. Then in the OnModelCreating override, use the prefix for the table names:
public class MyContext : DbContext
{
private readonly string _prefix;
public MyContext(string prefix)
{
_prefix = prefix;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types()
.Configure(entity => entity.ToTable(_prefix + entity.ClrType.Name));
}
}
Related
we have regularly changing Database Data(every two weeks to once a month).
Usually the latest data has to be used, but in some special cases older data is necessary.
The current info which version has to be used atm is stored in another table.
The Database looks like this, versioned Schema-Names with the same tables beneath it.
YYYYMMDD+Revision
myshema_202001011
table1
myshema_202002011 and so on
table1
myshema_202003011 and so on
table1
I have build a Aspnet core (2.2) service with two DbContext classes,
one for the static schemas that gets the current version to use and one for the changing schemas that accesses those data.
The static DbContext works just fine.
The problem is, even when i use the changing contaxt with a using like,
using (var _context = new ChangingDbContext()){}
the constructors and OnConfiguring are executed each time but the OnModelCreating method is only executed once.
This leads to NOT updating to the current schemas.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasAnnotation("ProductVersion", "2.2.6-servicing-10079");
modelBuilder.Entity<my_table>(entity =>
{
entity.HasKey(e => e.key_adr);
entity.ToTable("mytable", $"myshema{mySchemaVersion}");
});
}
Has anyone a clue how to get a "really" new context where OnModelCreating is executed every time?
Or maybe another solution how to handle those changing Schemas?
To continue from my comment. The below db table design allows you or users add as many as new fields to an object as they want. And I think it gives most flexible structure.
Let's assume in a eCommerce system, we provide 3 fields (Name, Code, Price) for the product. But we also allow users want to add their custom fields to their products (e.g. Promotion1Price, Promotion2Price, Discount, ...)
PRODUCT (ProductId, Name, Code, Price)
CUSTOMEFIELD (FieldId, FieldName, FieldType)
PRODUCT_CUSTOMFIELD (ProductId, FieldId, FieldValue)
Let me know if this doesn't serve your purpose right.
Solved by this Answer https://stackoverflow.com/a/41985226/6692289
Quote from Example in case it gets deleted.
Derived DbContext that replaces it's ModelCacheKey (and factory) with
a Custom one.
class MyDbContext : DbContext
{
public MyDbContext(string schema)
{
Schema = schema;
}
public string Schema { get; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options
.UseSqlServer("...")
.ReplaceService<IModelCacheKeyFactory, MyModelCacheKeyFactory>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(Schema);
// ...
}
}
The factory that creates the Context with a specific key.
class MyModelCacheKeyFactory : IModelCacheKeyFactory
{
public object Create(DbContext context)
=> new MyModelCacheKey(context);
}
The custom ModelCacheKey per context.
class MyModelCacheKey : ModelCacheKey
{
string _schema;
public MyModelCacheKey(DbContext context)
: base(context)
{
_schema = (context as MyDbContext)?.Schema;
}
protected override bool Equals(ModelCacheKey other)
=> base.Equals(other)
&& (other as MyModelCacheKey)?._schema == _schema;
public override int GetHashCode()
{
var hashCode = base.GetHashCode() * 397;
if (_schema != null)
{
hashCode ^= _schema.GetHashCode();
}
return hashCode;
}
}
And using the Context like.
using (var _myContext = new MyDbContext(_schemaNameToUse)
{
}
I want to use annotations for setting the default value for my properties in Entity Framework Core. The issue is that the database is not setting the default values so the value is not being passed down to the database layer.
I want to do something similar to modelBuilder's HasDefaultValueSql:
[DefaultValue("400")]
public int LengthInMeters {get; set;}
How do you convert the below code to attributes?
modelBuilder.Entity<Patient>().Property(c => c.LengthInMeters).HasDefaultValueSql("400");
Using default values by themselves doesn't work. I want to use attributes alone without having to mess with the migrations.
Problems: I've tried other methods with EF but Entity Framework Core doesn't have some items. Such as modelBuilder.Conventions nor AttributeToColumnAnnotationConvention nor CSharpMigrationCodeGenerator nor modelBuilder.Properties()
This is what I ended up doing, if someone has a cleaner not as intensive way of implementation let me know.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
var memberInfo = property.PropertyInfo ?? (MemberInfo)property.FieldInfo;
if (memberInfo == null) continue;
var defaultValue = Attribute.GetCustomAttribute(memberInfo, typeof(DefaultValueAttribute)) as DefaultValueAttribute;
if (defaultValue == null) continue;
property.SqlServer().DefaultValue = defaultValue.Value;
}
}
}
I can set the default value in the database using the default value attribute
[DefaultValue("400")]
public int LengthInMeters {get; set;}
Struggled a while getting this job done in another way using EF-Core conventions. I discovered a way to add so called "Plugins" which implement the IConventionSetPlugin interface with which you can add custom conventions. It needs some additional code to get EntityFramework to use the plugin.
But first things first, let's create our PropertyAttributeConvention.
public class DefaultValueAttributeConvention : PropertyAttributeConventionBase<DefaultValueAttribute>
{
public DefaultValueAttributeConvention(ProviderConventionSetBuilderDependencies dependencies) : base(dependencies) { }
protected override void ProcessPropertyAdded(IConventionPropertyBuilder propertyBuilder, DefaultValueAttribute attribute,
MemberInfo clrMember, IConventionContext context)
{
propertyBuilder.HasDefaultValue(attribute.Value, fromDataAnnotation: true);
}
}
Here we just say the ef propertybuilder to use the default value defined in our [DefaultValue] attribute.
To add the convention we need to create a custom plugin class:
public class CustomConventionSetPlugin : IConventionSetPlugin
{
public ConventionSet ModifyConventions(ConventionSet conventionSet)
{
conventionSet.PropertyAddedConventions.Add(new DefaultValueAttributeConvention(null));
return conventionSet;
}
}
For our plugin to get used, we have to create an ef extension class (which itself contains another ExtensionInfo class)
public class CustomDbContextOptionsExtension : IDbContextOptionsExtension
{
public void ApplyServices(IServiceCollection services)
{
services.AddSingleton<IConventionSetPlugin, CustomConventionSetPlugin>();
}
public void Validate(IDbContextOptions options) { }
public DbContextOptionsExtensionInfo Info => new CustomDbContextOptionsExtensionInfo(this);
private class CustomDbContextOptionsExtensionInfo : DbContextOptionsExtensionInfo
{
public CustomDbContextOptionsExtensionInfo(IDbContextOptionsExtension extension) : base(extension) { }
public override long GetServiceProviderHashCode() => 0;
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) { }
public override bool IsDatabaseProvider => false;
public override string LogFragment => "";
}
}
In the extension class we're adding our plugin class to the EF-ServiceCollection.
The last step is to go to our DbContext and add our extension. This can be done in the OnConfigure method:
public class MyDatacontext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(new CustomDbContextOptionsExtension());
}
}
Now the [DefaultValue] attribute can be used on our entity properties.
If we want to add different custom conventions we dont have to create all that extension/plugin classes again. Just create a new convention class and add it through our existing plugin class to the convetionSet.
Install Microsoft.EntityFrameworkCore.Relational package, it should solve most of your migration issues when moving to EF core.
I want to set database schema name for a table, but the only way do it is using method ToTable:
modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
However I don't want to set table name explicitly, I need something like this:
modelBuilder.Entity<MyEntity>().ToSchema(schemaName);
Can anyone help me with it?
Using this extension method:
using System.Data.Entity.ModelConfiguration;
public static class ModelConfigurationHelper
{
public static EntityTypeConfiguration<T> ToSchema<T>(this EntityTypeConfiguration<T> config, string schema)
{
return config.ToTable(typeof(T).Name, schema);
}
}
you can do:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().ToSchema("someSchema);
}
I use entity framework code first to work with my database.
I have several tables with different names but same structure, and this tables dynamically appears in database. How could I map EntityFramework to one of that tables at run-time and use data from just like I work this over entities of DbContext?
What I've done to make it work:
For example, my class what describes structure of dynamically created table is SetElement.
Here is my context:
public class DataContext : DbContext
{
public DataContext()
: base("RepositoryConnectionString") { }
string setElementsTableId; // the name of table that need to be dynamicly mapped to
// Enforce model recreating
public DataContext(string setElementsTableId)
: this()
{
this.setElementsTableId = setElementsTableId;
}
/* some other entities */
public DbSet<Entities.SetElement> SetElements { get; set; } // dynamicly mapped entity
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
/* come configurations */
if (!string.IsNullOrEmpty(setElementsTableId))
{
modelBuilder.Entity<Entities.SetElement>().Map(x => x.ToTable(setElementsTableId)); // map SetElements property to dynamicly created table
}
}
}
How I use this:
public static void AddSetElements(ICollection<SetElement> setElements, string tableId)
{
using (ctx = new DataContext(tableId)) // configere DataContext to map tableId table for entity SetElements
try
{
var num = ctx.SetElements.Count();
ctx.SetElements.AddRange(setElements);
ctx.SaveChanges();
}
catch (Exception e)
{
}
}
I have also some methods to get, udtate and remove data from dynamicly created tables that are same to AddSetElements.
All works just as I wish but only if AddSetElements runs first, because at the first datacontext creating DbContext.OnModelCreating runs and configure all mappings. But next instance creation doesn't call DbContext.OnModelCreating.
So, my question is: how to call DbContext.OnModelCreating everytime of creating an instance of DataContext then I use DataContext(string setElementsTableId) to create it?
I know, my question is similar to 'dynamic table mapping in EF' but I found nothing in the results.
By the way. If you know another way to solve my problem, you are welcome.
There is a built-in feature which may address your issue : `IDbModelCacheKey ; the implementation of which is to be registered in your configuration.
The point is to generate a different key for your different contexts.
I would go for something like :
First, the configuration
public class EntityFrameworkConfiguration: DbConfiguration
{
public EntityFrameworkConfiguration()
{
this.SetModelCacheKey(ctx => new EntityModelCacheKey((ctx.GetType().FullName + ctx.Database.Connection.ConnectionString).GetHashCode()));
}
}
Then the implementation of the IDbModelCacheKey
public class EntityModelCacheKey : IDbModelCacheKey
{
private readonly int _hashCode;
public EntityModelCacheKey(int hashCode)
{
_hashCode = hashCode;
}
public override bool Equals(object other)
{
if (other == null) return false;
return other.GetHashCode() == _hashCode;
}
public override int GetHashCode()
{
return _hashCode;
}
}
Finally, your DataContext
public class DataContext : DbContext
{
string setElementsTableId;
// use the setElementsTableId as extended property of the
// connection string to generate a custom key
public DataContext(string setElementsTableId)
: base(ConfigurationManager.ConnectionStrings["RepositoryConnectionString"]
+ "; Extended Properties=\"setElementsTableId=" + setElementsTableId + "\"")
{
this.setElementsTableId = setElementsTableId;
}
public DbSet<Entities.SetElement> SetElements { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (!string.IsNullOrEmpty(setElementsTableId))
{
modelBuilder.Entity<Entities.SetElement>().Map(x => x.ToTable(setElementsTableId));
}
}
}
I hope this will be of some help
Look like nobody knows answer...
Otherwise, one man told me that my question is meaningless because of storage data in several tables will not give any achievement. More better to add indexes to database, partitioning table or something else. In other words this is Database Management System problem. But if some one knows answer I'll be very pleasured to hear something about EF hack.
I'm using EF5 Code First, I have entities named XXXEntity which is based on Entity class, in Entity class there is Id property.
Here is the problem, normaly EF will create table for each entity named XXXEntities and having Id field. What I want is the table should named XXX (without Entities) and the Id should be XXXId. How to do that at once by using convention. I know I can override the table name, and Id name one by one. But it is a bit boring and not reusable, is there any better way to do that using convention or something on EF 5?
UPDATE
i read about custom code first conventions but not sure is this an out dated page or a non implemented feature. because i couldn't found the Properties<T>() method on EF 5
No, you can't do that with EF 5, your link is a future feature for EF 6 see here
but you can do that with reflection easily, for reusability you can make it as an extension method of DbModelBuilder, a bit slow but it solve your case. here what you can do:
public static class MyCustomNamingConvention
{
public static void ToMyDatabaseNamingConvention(
this DbModelBuilder mb, IEnumerable<Type> entities)
{
foreach (var entity in entities)
{
var mi = typeof(MyCustomNamingConvention)
.GetMethod("MyNamingConvention")
.MakeGenericMethod(entity);
mi.Invoke(null, new object[] { mb });
}
}
public static void MyNamingConvention<T>
(DbModelBuilder mb) where T : Entity
{
var tableName = typeof(T).Name.Replace("Entity", "");
mb.Entity<T>().HasKey(x => x.Id);
mb.Entity<T>().Property(x => x.Id).HasColumnName(tableName + "Id");
mb.Entity<T>().ToTable(tableName);
}
}
Simply use it on your DBContext on OnModelCreating method
protected override void OnModelCreating(DbModelBuilder mb)
{
base.OnModelCreating(mb);
mb.ToMyDatabaseNamingConvention(GetEntities());
}
Note:
Remember to add appropriate namespace of the
MyCustomNamingConvention.
GetEntities() is a method to iterate your entity (remember to skip
the Entity base class)
Use this to remove table pluralizing:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
I cant seem to find a convention that you can disabled for the column Id name. Have a look at http://msdn.microsoft.com/en-us/library/gg696316(v=vs.103).aspx