Archive

Archive for the ‘Entity Framework’ Category

Using Entity Framework Core with WPF on .NET Core 3.0

19/03/2019 2 comments

We talked about how to configure a .NET Core 3.0 WPF application in the same way as we do with ASP.NET Core. We can keep using this approach also if we need to use Entity Framework Core. It is important to note that tipically we use an external service to perform data access (for example, a REST Web APIs backend), but in some cases (especially if we’re working with a legacy system) it might be necessary to access a database directly from within our application.

And the story is again the same: you use the same code of ASP.NET Core. Let’s see how to do that. As always, take this sample as reference. First of all, we need to add the NuGet package for Entity Framework Core:

Adding Entity Framework Core to .NET Core 3.0 WPF application

Adding Entity Framework Core to .NET Core 3.0 WPF application

In this example, we’re using Entity Framework with SQL Server, but of course we can use every database supported by this OR/M.

Now, let’s add a connection string in the appsettings.json file:

{
  "ConnectionStrings": {
    "SqlConnection": ""
  }
}

Then, we must create our data context, i.e., a class that inherits form DbContext and provides access to the database:

public class DataContext : DbContext
{
    // DbSet properties declarations...

    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
    }
}

The DataContext class contains a constructor that takes a DbContextOptions as argument. It is used to pass configuration settings to the Context via Dependency Injection. For the rest, we can add to the class the usual DbSets we are accustomed to and configure the model by overriding the OnConfiguring and OnModelCreating methods.

Finally, we need to register the DbContext in the ServiceProvider collection, like we did in the previous post:

private void ConfigureServices(IServiceCollection services)
{
     services.AddDbContext<DataContext>
        (options => options.UseSqlServer(
                    Configuration.GetConnectionString("SqlConnection")));

            
     services.AddTransient(typeof(MainWindow));
}

At lines 4-5 we setup the DataContext to use SQL Server with the connection string from the appsettings.json file we defined before.

Now we’re ready to use the DataContext in every Window we need it. For example:

private readonly DataContext dataContext;

public MainWindow(DataContext dataContext)
{
    InitializeComponent();

    this.dataContext = dataContext;
}

As we can see, also in this case the approach is the same we use with ASP.NET Core. Of course, we can choose to pass the DataContext to a business service, instead of accessing it directly: as long as the service is itself in the IoC container, everything works in the same way.

Advertisements
Categories: .NET Core, C#, Entity Framework, WPF

How to specify table schema name with Entity Framework Code First

07/01/2013 Comments off

Using Entity Framework Code First approach, if we need to specify a schema name other than the default dbo, we need to override the OnModelCreating method of DbContext class:

public class MyDbContext : DbContext
{
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<MyTable1Entity>().ToTable("MyTable1", schemaName: "myschema");
    base.OnModelCreating(modelBuilder);
  }
}

In this example, we map the MyTable1Entity object to MyTable1 table, specifying that it is contained in the myschema schema.

Categories: ADO.NET, C#, Entity Framework