How to define a composite key, with EF 7, in F# - entity-framework

I want to replicate this, from C#:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<VehicleFeature>().HasKey(vf=> new {vf.VehicleId, vf.FeatureId});
}
to F#.
The one solution I had found on this site was:
fun x -> (x.VehicleId, x.FeatureId) :> obj)
which doesn't work.

Related

How to register Ef core db context with Castle windsor?

I " ve got a project under .net core. I want to register Ef Core Context with Castle windosr But I couldn 't find a solution to EfCore Wireup context in .net core. Thank you.
If you want to do this , first you need to know that you have a Context that has a DbContextOptionsBuilder parameter that has a DbContextOptionsBuilder parameter if you have added these constructor , you need to register this too , and now the code I " ve written below makes you less self - sufficient to use the OnConfiguring method.
public static class DbContextFactoryBuilder
{
public static IDbContext Create(string connectionString)
{
var result = new MyDbContext(new DbContextOptionsBuilder().UseSqlServer(connectionString).Options);
return result;
}
}
and code for register in castle.
container.Register(Component.For<MyDbContext>().UsingFactoryMethod(c => DbContextFactoryBuilder.Create(#"---your connection string---")));
Another alternative is:
public class MyContext : DbContext
{
private readonly string connectionString;
public MyContext (string connectionString)
{
this.connectionString = connectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(this.connectionString);
}
}
Component.For<MyContext>().DependsOn(Property.ForKey<string>().Eq("your connection string")));

How to Resolve multiple DBContext call using generic UnitOfWork<TContext> in Autofac

Hi I have created my UnitOfWork as generic and at runtime it should create new instance of DB context with DBContextOption Builder on the basis of TContext passing I have registered Mention DB Context in autofac but how to resolve this at DB Context Constructor Level
DB Context 1 Implemetation
public class DBContext1 : DbContext
{
public DBContext1(DbContextOptions<DBContext1> options1) : base(options1)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
DB Context 2 Implemetation
public class DBContext2 : DbContext
{
public DBContext2(DbContextOptions<DBContext2> options2) : base(options2)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
IUnitOfWork interface Implemetation
public interface IUnitOfWork<TContext> where TContext : DbContext, IDisposable
{
}
UnitOfWork class Implemetation
public class UnitOfWork<TContext> : IDisposable, IUnitOfWork<TContext> where TContext : DbContext, new()
{
private DbContext _context;
public UnitOfWork()
{
_context = new TContext();
}
}
StartUp Class Implemetation
public class Startup
{
protected IConfiguration _configuration { get; set; }
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFrameworkSqlServer()
.AddDbContext<DBContext1>(options =>
options.UseSqlServer(_configuration.GetConnectionString("DBContext1")))
.AddDbContext<DBContext2>(options =>
options.UseSqlServer(_configuration.GetConnectionString("DBContext2")));
/* Autofac DI Configuration with registering DBContext/DataModule/ServiceModule to it */
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterInstance(_configuration).AsImplementedInterfaces().ExternallyOwned();
var autoFacOptions1 = new DbContextOptionsBuilder<DBContext1>().UseSqlServer(_configuration.GetConnectionString("DBContext1")).Options;
var autoFacOptions2 = new DbContextOptionsBuilder<DBContext2>().UseSqlServer(_configuration.GetConnectionString("DBContext2")).Options;
containerBuilder.Register(c => new DBContext1(autoFacOptions1)).As<DbContext>();
containerBuilder.Register(c => new DBContext2(autoFacOptions2)).As<DbContext>();
containerBuilder.RegisterModule<DataModule>();
containerBuilder.RegisterModule<ServiceModule>();
containerBuilder.Register<String>(c => Guid.NewGuid().ToString())
.Named<String>("correlationId")
.InstancePerLifetimeScope();
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}/{id?}");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
I am able to achieve multiple DBContext Call as required but I have to create Default constructor & connection string in DB context like mention below
DB Context 1 Implemetation
public class DBContext1 : DbContext
{
public DBContext1()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Data Source=Server;Database=DB;User Id=UserID;Password=Password;Integrated Security=False;MultipleActiveResultSets=true;");
}
public DBContext1(DbContextOptions<DBContext1> options1) : base(options1)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
DB Context 2 Implemetation
public class DBContext2 : DbContext
{
public DBContext2()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Data Source=Server;Database=DB;User Id=UserID;Password=Password;Integrated Security=False;MultipleActiveResultSets=true;");
}
public DBContext2(DbContextOptions<DBContext2> options2) : base(options2)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Please help me to call parameterised constructor of DBContext1 & DBContext2 using autofac dependency resolver
Well, if you're using autofac to resolve dependencies then why are you trying to do its job for it? :) That's the main problem with your code.
First of all, you don't need to register IConfiguration explicitly. It is already registered in the IServiceCollection that's passed to ConfigureServices() method and will be automatically picked up by autofac during containerBuilder.Populate(services) call. You can just remove this registration and nothing will change.
Further, you're registering both your DbContexts twice - in the service collection and in the autofac container builder. This is not necessary as the latter will effectively replace the former. Also, it creates confusion about what is registered where and how this whole this is going to work. It's better to pick one method of registration and stick with it.
Next problem: how are you going to unit test your unit of work? It has hard dependency on DbContext whose lifecycle you cannot control in tests. This is exactly what you need autofac for: manage component's dependencies for you allowing you to concentrate on the component's purpose and not on the secondary stuff.
Next confusion point is here:
containerBuilder.Register(c => new DBContext1(autoFacOptions1)).As<DbContext>();
containerBuilder.Register(c => new DBContext2(autoFacOptions2)).As<DbContext>();
By doing this you are effectively replacing first db context registration with the second. From this point there is no way to inject DBContext1 anywhere in your application. EDITED: You still can inject collection of DbContext derivative implementations and find DBContext1 among them... but that would look very weird.
All in all, this can be done in much more clean and straightforward way.
Startup
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var builder = new ContainerBuilder();
builder.Register(c => c.CreateDbContextOptionsFor<DBContext1>("DBContext1")).As<DbContextOptions<DBContext1>>().SingleInstance();
builder.Register(c => c.CreateDbContextOptionsFor<DBContext2>("DBContext2")).As<DbContextOptions<DBContext2>>().SingleInstance();
builder.RegisterType<DBContext1>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<DBContext2>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<SomeComponent>().As<ISomeComponent>().InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>)).InstancePerLifetimeScope();
builder.Populate(services);
var container = builder.Build();
return new AutofacServiceProvider(container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
}
}
CreateDbContextOptionsFor helper implementation. It is introduced in order to make Startup code concise and more readable. It can probably be improved even further by making use of autofac's parameterized factory instead of new DbContextOptionsBuilder<TContext>(), but I'm not sure if there's a point in it in this case.
public static class DBExtentions
{
public static DbContextOptions<TContext> CreateDbContextOptionsFor<TContext>(this IComponentContext ctx,
string connectionName) where TContext : DbContext
{
var connectionString = ctx.Resolve<IConfiguration>().GetConnectionString(connectionName);
return new DbContextOptionsBuilder<TContext>().UseSqlServer(connectionString).Options;
}
}
UnitOfWork
public class UnitOfWork<TContext> : IUnitOfWork<TContext> where TContext : DbContext
{
private TContext _context;
public UnitOfWork(TContext context)
{
_context = context;
}
}
Injecting and using unit of work
public class SomeComponent : ISomeComponent
{
private readonly IUnitOfWork<DBContext1> _uow;
public SomeComponent(IUnitOfWork<DBContext1> uow)
{
_uow = uow;
}
public void DoSomething()
{
_uow.DoWhatever();
}
....

How to run a query in Entity Framework OnModelCreating

Any ideas how to run a query in OnModelCreating?
I am trying to run a query and then ignore a column in the entity based on that.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var d = this.Database.SqlQuery<int?>(#"select 1 from sys.columns where Name = N'columnname' and Object_ID = Object_ID(N'tablename')").SingleOrDefault();
if(d == null)
{
depEntity.Ignore(d => d.colmnname);
}
}
I am getting the following error:
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe. (See inner exception for details.)
System.InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
I think you should have two context classes for both cases. And your query should be invoked inside Factory to select class of which instance should be returned:
public class CommonContext : DbContext
{
//common stuff...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//common stuff...
}
}
public class IgnoreContext : CommonContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DepEntity>().Ignore(d => d.colmnname);
}
}
public ContextFactory()
{
public CommonContext CreateContext()
{
var ctx = new CommonContext();
var d = ctx.Database.SqlQuery<int?>(#"select 1 from sys.columns where Name = N'columnname' and Object_ID = Object_ID(N'tablename')").SingleOrDefault();
if(d != null)
return ctx;
return new IgnoreContext();
}
}

System.Data.Entity.ModelConfiguration missing in EF core

Trying to load all the configurations dynamically on OnModelCreating for Entity framework core.
what is the other way around if ModelConfiguration is missing.
It's even easier in Core 2.0 now
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace MyApp.DAL.EntityConfigurations
{
public class StudentConfiguration : IEntityTypeConfiguration<Student>
{
public void Configure(EntityTypeBuilder<Student> modelBuilder)
{
modelBuilder.Property(f => f.Name).IsRequired();
}
}
}
Then in your db context:
public DbSet<Student> Students{ get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customizations must go after base.OnModelCreating(builder)
builder.ApplyConfiguration(new StudentConfig());
builder.ApplyConfiguration(new SomeOtherConfig());
// etc.
// etc..
}
I've just stumbled across this question as I was searching for the answer myself. I found that it is not (yet?) implemented in EF Core but can be implemented yourself fairly easily.
You can create one of these:
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Microsoft.EntityFrameworkCore
{
public abstract class EntityTypeConfiguration<TEntity> where TEntity : class
{
public abstract void Map(EntityTypeBuilder<TEntity> modelBuilder);
}
public static class ModelBuilderExtensions
{
public static void AddConfiguration<TEntity>(this ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration) where TEntity : class
{
configuration.Map(modelBuilder.Entity<TEntity>());
}
}
}
And then you can create a configuration for the entity itself: -
using Microsoft.EntityFrameworkCore;
using Project.Domain.Models;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Project.Persistance.EntityConfigurations
{
public class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
{
public override void Map(EntityTypeBuilder<MyEntity> modelBuilder)
{
modelBuilder
.Property();//config etc
}
}
}
You can then load all your configurations somewhere (there's probably both a better way and a better place for doing it... but this is what I did): -
using Microsoft.EntityFrameworkCore;
using Project.Domain.Models;
using Project.Persistance.EntityConfigurations;
namespace Project.Persistance
{
public class MyDbContext : DbContext
{
// Normal DbContext stuff here
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.AddConfiguration(new MyEntityConfiguration());
}
}
}

Unit testing with Effort

I'm currently trying to unit test an context class of the Entity Framework with the "Effort" framework (http://effort.codeplex.com/wikipage?title=Tutorials&referringTitle=Home).
This is how my test currently looks:
[TestMethod]
public void GetUseraccountsForRealTest()
{
DbConnection connection = Effort.DbConnectionFactory.CreateTransient();
SqlContext context = new SqlContext(connection);
context.TaskComment.Add(new TaskComment() { Id = 1, Message = "Test" });
}
The last line isn't working. Nothing happens.
This is how my SqlContext class looks:
public class SqlContext : DbContext
{
...
public IDbSet<TaskComment> TaskComment { get; set; }
...
//Constructor used by webserver
public SqlContext(string connectionString) : base(connectionString)
{
}
//Constructor used for unit testing
public SqlContext(DbConnection connection) : base(connection, true)
{
this.Configuration.LazyLoadingEnabled = false;
}
///
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
Anyone got an idea how I can solve this problem? Don't have any experience with "Effort" and there's not too much documentation. :-(
Solved the problem on my own.
I didn't use IDbSet in my context, butt just DbSet which lead to some difficulties.