I use ABP, and I am getting an error:
Filter name MustHaveTenant not found
I don't need multiple tenants.
I checked the ABP document, and I did use this code:
Configuration.UnitOfWork.OverrideFilter(AbpDataFilters.MustHaveTenant, false);
But I still get the error.
DbContext code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditLog>().Property(t => t.RequestId).HasMaxLength(50);
modelBuilder.Entity<AuditLog>().Property(t => t.Method).HasMaxLength(20);
}
First if you don't need to use MiltiTenancy you can turn it off on your ProyectCoreModule
Configuration.MultiTenancy.IsEnabled = PuntoVentaConsts.MultiTenancyEnabled;
In this line you can change it to false or change the const property value to false
And you're getting the filter error because you omitted the call to
base.OnModelCreating(modelBuilder); in your OnModelCreating() method, it has to be like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AuditLog>().Property(t => t.RequestId).HasMaxLength(50);
modelBuilder.Entity<AuditLog>().Property(t => t.Method).HasMaxLength(20);
}
Related
I need to apply a global query filter in my EF DbContext.
The value to check against is retrieved by an async operation.
So something like this.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasQueryFilter(async u => u.CustomerId == await GetIdAsync());
}
Of course this doesn't work, as HasQueryFilter expects a different lamda expression.
And I guess, that adding async to the OnModelCreating will bring me into troubles, too.
protected override async void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var id = await GetIdAsync();
modelBuilder.Entity<User>().HasQueryFilter(u => u.CustomerId == id);
}
Any ideas, how to solve this?
Define property CustomerId in your DbContext:
public class MyDbContext: DbContext
{
public int? CustomerId { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasQueryFilter(u => CustomerId == null || u.CustomerId == CustomerId);
}
}
Assign property in controller call:
context.CustomerId = await context.GetIdAsync();
And then you can sefely use Query Filter.
I have an existing model, SomeModel, which contains a property Prop. I'd like to enforce that the values of Prop be unique.
To this end, I'm adding the following to my model:
public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeModel>()
.HasAlternateKey(a => a.Prop);
}
but ef migrations isn't picking up the change.
How can I correct this?
This works for me with EF Core on an already existing model. add-migration picks up the change.
public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeModel>()
.HasIndex(u => u.Prop)
.IsUnique();
}
Is there anyway to cascade delete false in the entity framework config settings. For now I have done using fluent API but there are multiple place where cascade delete need to be set false, so is there any single point where I can set for all classes, as I don't have any need of it.
Thanks.
public partial class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//Set cascade options here also
}
For some reason code-first EF7 (vNext) will not use/find the plural form of my table. I have tried adding the table attribute to the model but it does not solve the problem.
[Table("Units")]
public class Unit
If I name the table Unit then no problem. If I name the table Units then it's not found.
What am I doing wrong or missing?
Thank you.
This is how I resolved:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Unit>().ToTable("Units");
}
For Entity Framework 7 beta1, I solved this issue by this way:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Unit>().ForRelational(rb =>
{
rb.Table("Units");
});
}
Entity Framework 7 is being configured using the Fluent API. I created an extension method which maps the table names to their plural form, with the intention to reproduce the EF 6 behavior and to be able to use my existing database while working with EF7.
public static class ModelBuilderExtensions
{
public static void PluralizeNames(this ModelBuilder modelBuilder)
{
var types = modelBuilder.Model.EntityTypes;
foreach (var type in types.Where(type => type.ClrType != null))
{
modelBuilder.Entity(type.ClrType)
.ForRelational()
.Table(type.ClrType.Name.Split('`')[0].Pluralize());
};
}
}
Please note the .Pluralize() extension method. This may be Humanizer you're using or any other extension method which pluralizes your string. (I shamelessly copied https://github.com/srkirkland/Inflector/blob/master/Inflector/Inflector.cs in my project to be able to compile my project with the DNX Core.)
The .Split() part is to deal with the type.ClrType.Name which can output stuff like IdentityUserRole`1.
You can use it like this in your DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.PluralizeNames();
}
Ps; this works for me
Now the ToTable and ForRelational both are missing in beta5 of EF7. So I used the below code.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Role>().ForSqlServer().Table("Role");
}
You need to add "Microsoft.EntityFrameworkCore.Relational" in your project.json and restore your package.
.NET core is broken down in to small pieces for less memory foot print. So you need to explicitly tell what you want.
I have been using EF Code First CTP5 with dedicated mapping classes for each entity, like this:
public class UserMapping : EntityTypeConfiguration<User>
{
public UserMapping()
{
ToTable("Users");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("user_id");
Property(t => t.Name).HasColumnName("name");
}
}
And loading them the way Jonas Cannehag describes here:
http://areaofinterest.wordpress.com/2010/12/08/dynamically-load-entity-configurations-in-ef-codefirst-ctp5/
But that doesn't work in RTM and I haven't been able to figure out how to use dedicated mapping classes. Have you? :-)
public class DataContext : DbContext
{
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserMapping());
base.OnModelCreating(modelBuilder);
}
}
dynamic version (probably should test extensive before putting in production)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister =
Assembly.GetExecutingAssembly().GetTypes().Where(
type =>
type.BaseType.IsGenericType &&
type.BaseType.GetGenericTypeDefinition() == typeof (EntityTypeConfiguration<>));
foreach (object configurationInstance in typesToRegister.Select(Activator.CreateInstance))
{
modelBuilder.Configurations.Add((dynamic) configurationInstance);
}
base.OnModelCreating(modelBuilder);
}