Tag: entity-framework

Identity mapping to a view with Entity Framework

It’s a common enough problem: you have a view that contains no primary key identifier, or even any combination of columns that guarantees each row to be unique. And you want to use this view in Entity Framework. What do you do? It’s been suggested that you modify the view to add a generated primary […]

EFInteractiveViews With Unit Of Work Raise An Exception Second Run

I have a big MVC & EF6 project. I have decided to use EFInteractiveViews to Pre-Generated views. But it’s raise “MappingViewCacheFactory is already set and cannot be modified.” exception at second run. How can i solve this problem? Thanks private MyContext _context; public UnitOfWork() { _context = new MyContext(); InteractiveViews .SetViewCacheFactory(_context, new SqlServerViewCacheFactory(_context.Database.Connection.ConnectionString)); }

Entity Framework self referencing primary key on insert

I am using entity framework and I have a scenario where I was handed down a database that is not designed properly and I cannot make any modifications to it, so please bare with me. When I insert a new record and it auto generates an auto identity value. I then have to subsequently update […]

Entity Framework with Oracle AND SQL (switch automatically)

I maintain an asp.net MVC application which uses Entity Framework (database first) and connects to an oracle database. My question: How can I switch between oracle and SQL server using entity framework? The meaning has the ability to get data from both oracle and SQL server. Also, the switch from oracle to SQL server (if […]

Entity Framework reverse POCO generator and Table-Per-Hierarchy

I have an existing SQL Server database using Entity Framework code-first with a table-per-hierarchy option. This means there is a Discriminator column in table Persons representing the different possible types – let’s say “Doctor” and “Nurse”. Now, I want to move over to managing the database in a Visual Studio Database Project (SSDT). And at […]

SqlQuery in EntityFramework with sql command including non mapped table

I want to run a SQL statement in a EntityFramework DbContext, but I don’t get the expected result compared with the same query executed in SQL Management Studio. Zero result run in code and 3 run in SQL-MS. The calling methods looks like: internal List<AddressZ> BaseAddresses(string email) { string sql = @”select * from [Address] […]

I am confused in two ways to create one to many relationship in code first migration . Which one to choose?

I have used two ways to create one to many relationships in code first migration and I do not know what is the difference between each of them. First method: public class Student { public int Id { get; set; } public string Name { get; set; } } public class Course { public int […]

AlterColumn fails for indexed properties on db migration in entity framework

I have the following Entity for which I have successfully created the table in my db. Now on migration I am changing the ‘CreatedOn’ column to have default value.But every time I run the Alter command I get the following Error. Entity public class MyTeam { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } […]

How to set CONTEXT_INFO on Entity Framework connection

I’m working on isolating store data in a shared database in an Entity Framework-based application. I want to use SQL Server 2016 row-level security but I’d prefer to have my database connections all use a single user. So I want to set the SQL Server CONTEXT_INFO to a store number for all of my Entity […]

Using cast when joining tables

I am using a legacy database at my job and I need to join two tables on columns which are different data types. One is int and the other one is long, so when Entity framework translates LINQ join to SQL expression I got something like this in JOIN condition: CAST( [Extent1].[KundeID] AS bigint) = […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.