Tag: entity-framework

Seeding my DB with Entity Framework DateTimes

I am using Code First Workflow with Entity Framework 6.1.3 and I am trying to seed my database with some data by adding a migration like so: public partial class SeedDatabaseWithBusinesses : DbMigration { public override void Up() { Sql(“INSERT INTO Businesses (GooglePlaceId, Name, CurrentMenu, LastUpdated)” + ” VALUES (‘googlePlaceIdHere’, ‘Examples Businesses Name’, ‘Fish and […]

Entity Framework added 3 foreign keys for 1 relationship

I have a simple User Class public class User { public int ID { get; set; } [Required] public virtual ApplicationUser LoginID { get; set; } [Required] public string Name { get; set; } public string JobTitle { get; set; } [DefaultValue(UserRole.Standard)] public UserRole Role { get; set; } public virtual Company Company { get; […]

Entity Framework SQL

I hope anyone can help me. How do I build up a query like this in Entity Framework in C#. My SQL query looks like this: select Id, dbo.AccessRight(Id, 5) from Customers where Id in (select top 10000 Id from Customers where dbo.AccessRight(Id, 5) > 0) The second select statement should be replaced in code […]

Calculating SUM on 3 related tables

I have three tables that are in 1-to-many relationships. Since my business scenario is not easy to explain, I am going to use a more familiar terms: Customers -> Invoices -> InvoiceDetails Lets assume that there exist Customers.Value1, Invoices.Value2, InvoiceDetails.Value3, all of type double (real). I need to get the summaries of Value1, Value2 and […]

Two foreign keys reference one table and null able foreign key

I am new to Database tables and relationships .I need some help for the below requirements Work flow 1. Hospital will have Male Patient 2. Hospital will have Female Patient 3. Hospital Will have Couple Patient but in RegTable it will stored as separate record for male and female. For the above requirements i have […]

Passing DateTime parameter to stored procedure using EF(DB first)

I created a stored procedure in SQL Server 2012 ALTER proc [dbo].[select_alltypes] @cdin_startunstufdate1 DateTime = null, @cdin_startunstufdate2 DateTime = null AS BEGIN SET NOCOUNT ON; SELECT 0 as shipid, i.cdin_cdindexid, p.pinv_PerformaInvID, coalesce(i.cdin_serial, 0) as depno, coalesce(convert(datetime, left(convert(nvarchar, i.cdin_startunstufdate, 120), 10), 120),’-‘) as deidate, coalesce(i.cdin_goodsDesc, ‘-‘) as gooddesc, coalesce(i.cdin_Customdeclar, ‘-‘) as custdec, coalesce(i.cdin_NoofPackages, 0) as pkg, […]

Map foreign key to scalar property

I have 2 tables: Data and AdditionalData. Their relation is 1 to 1-0 (any Data can have 0 or 1 AdditionalData). For main context I have classes: class Data { public long Id {get;set;} // PK public string Name {get;set;} public AdditionalData AdditionalData {get;set;} // can be null } class AdditionalData { public long Id […]

Delete table from EF CodeFirst migration

In EF Code first, I want to drop one column from one table & then delete another table. After removing one column from class file, automatically one migration file generated. But how to delete table. what command need to fire? Do I need to delete complete class file & also remove following line from Context […]

Interception not working as expected with Entity Framework 6

I’m trying to follow the interception example shown here to get it working with EF 6 but running into a problem with the function RewriteFullTextQuery as shown in Figure 1. The interception seems to work but it does not actually execute the logic in the for loop of the RewriteFullTextQuery method because the cmd.Parameters.Count is […]

Linq to entites (EF6) return latest records of each group using Row number in T-SQL server

I’m using Linq to entities to get most recent updated record of each group. But actually when I checked in sql profiler my Ling query generated many sub-query so that It really take too much time to complete. To solve this performance problem, I already wrote native T-Sql mentioned below so that I’m looking for […]

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