Linq2DB: Bulk copy failing for float type column in MS Sql server - linq2db

I am trying to perform bulk copy for into a table that has few fields that are of type "float" in MS SQL Server. I noticed that after bulk copy completed, all these "Float" fields had value of "NULL". These fields allowed null value so these NULLs were there.
I traced the call in SQL Profiler and noticed that "INSERT BULK" command did not have these "float" fields.
I simplified my case to just simple object as below.
public class TestIt
{
public int Id{get;set;}
public string Title { get; set; } = $"Title{DateTime.UtcNow}";
public double? Price { get; set; }
}
The table has following schema.
CREATE TABLE [dbo].[TestIt](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](50) NULL,
[Price] [float] NULL,
CONSTRAINT [PK_TestIt] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
When I execute BulkCopy for a list of these object, I can see that "Title" field was copied correctly. Where was "Price" field always have NULL.
Is this some limitation in Linq2Db or I am doing something wrong.
When I use bulk copy with EF, it works fine.

Related

Entity Framework 6 - SQL Server and Oracle (SaveChanges)

I have an application which uses below (Employee) domain model for SQL Server and Oracle data provider. This model works fine for Oracle data provider, however the same gives error for SQL Server. And when I change the field "ID" from decimal to Int it works fine for SQL Server but gives error in Oracle.
On application configuration I am changing the data provider from i.e. from SQL Server to Oracle or vice-versa.
Cannot insert explicit value for identity column in table 'Employee' when IDENTITY_INSERT is set to OFF.*
ORA-01400: cannot insert NULL into ("Emp"."Employee"."ID")
public class Employee
{
[Key, Column(Order = 0)]
public decimal ID { get; set; }
public string NAME { get; set; }
}
if (ID == null || ID == 0)
{
Model.Employee obj = new Model.Employee();
obj.NAME = Name;
if (Convert.ToInt32(ConfigurationSettings.AppSettings["DataProviderType"]) == "Oracle")
obj.ID = GetNextId();
DbContext.Employees.Add(obj);
DbContext.SaveChanges();
}
SQL Server table script
CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](256) NULL,
CONSTRAINT [Employee_PK] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Oracle table script
CREATE TABLE Employee
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(256)
);
CREATE UNIQUE INDEX "PK_Employee" ON "Employee" ("ID");
ALTER TABLE "Employee" ADD CONSTRAINT "Employee_PK" PRIMARY KEY ("ID") ENABLE;
How can I handle this in my application i.e. in DOTNET Side?
With suggestion from #IvanStoev, I have change datatype from int to numeric IDENTITY(1,1) NOT NULL. in sql server. And also handled IDENTITY_INSERT error with below code,
modelBuilder.Entity<Employee>().Property(i => i.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
Hope this will help someone with similar issue.

Error loading association from controller in cakephp

I can't get this simple query right. I need to join my adresses table to my annonces table.
I supose this should be farly strait forward but I simply can't get it to work.
I firstly made my adresse table object like this
class AdressesTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Annonces', [
'foreignKey' => 'annonceId',
'joinType' => 'INNER',
]);
}
}
Then in my annonces controller I tryed to join the adresses like this
public function view($id = null)
{
if (!$id) {
throw new NotFoundException(__('Annonce invalide!'));
}
$query = $this->Annonces->find('all', ['contain' => ['Adresses']])->where(['id' => $id]);
$annonce = $query->firstOrFail();
$this->set(compact('annonce'));
}
But then I got this error :
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Adresses.annonce_id' in 'where clause'
Witch I don't understand why I got it since I have defined my foreign key in the AdressesTable class.
The query I would like to have at the end would look like this
SELECT *
FROM annonces
INNER JOIN adresses ON adresses.annonceId = annonces.id
WHERE annonces.id = #param
*PS. I know it wont be select * but rather select [all my column]*
EDIT : My table schema are as following
CREATE TABLE annonces(
id INT NOT NULL AUTO_INCREMENT, #PK
startDate DATE NOT NULL,
endDate DATE NOT NULL,
title VARCHAR(128) NOT NULL,
descript TEXT NOT NULL,
infoSupplementaire TEXT NULL,
premium BIT NOT NULL,
clientId INT NOT NULL, #FK
categorieId INT NOT NULL, #FK
CONSTRAINT pk_annonces_id PRIMARY KEY (id),
CONSTRAINT fk_annonces_clientId FOREIGN KEY (clientId) REFERENCES clients(id),
CONSTRAINT fk_annonces_categorieId FOREIGN KEY (categorieId) REFERENCES categories(id)
);
CREATE TABLE adresses(
id INT NOT NULL AUTO_INCREMENT, #PK
latitude DECIMAL(11,7),
longitude DECIMAL(11,7),
adresse VARCHAR(512),
annonceId INT NOT NULL, #FK
CONSTRAINT pk_adresses_id PRIMARY KEY (id),
CONSTRAINT fk_adresses_annonceId FOREIGN KEY (annonceId) REFERENCES annonces(id)
)
I solved my problem by renaming my column folowing cakephp convention and using any of the code from this answer
You can try
$query = $this->Annonces->find('all', ['contain' => ['Adresses']])->where(['Annonces.id' => $id]);
$annonce = $query->firstOrFail();
OR
public function view($id = null)
{
if (!$id) {
throw new NotFoundException(__('Annonce invalide!'));
}
$annonceEntity = $this->Annonces->get($id);
$query = $this->Annonces->find('all', ['contain' => ['Adresses']])->where(['Annonces.id' => $annonceEntity->id]);
$annonce = $query->firstOrFail();
$this->set(compact('annonce'));
}

Entity Framework query not generated

I have the following object model:
[Table("APA_QuestionProduct")]
public class QuestionProduct
{
[Key, ForeignKey("Question"), Column(Order=0)]
public int QuestionID { get; set; }
[ForeignKey("QuestionID")]
public Question Question { get; set; }
[Key, ForeignKey("Product"), Column(Order=1)]
public int ProductID { get; set; }
[ForeignKey("ProductID")]
public Product Product { get; set; }
}
Table:
USE [qbm]
GO
/****** Object: Table [dbo].[APA_QuestionProduct] Script Date: 5/21/2013 6:52:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[APA_QuestionProduct](
[QuestionID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
CONSTRAINT [PK_APA_QuestionProduct] PRIMARY KEY CLUSTERED
(
[QuestionID] ASC,
[ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[APA_QuestionProduct] WITH CHECK ADD CONSTRAINT [FK_APA_QuestionProduct_APA_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[APA_Product] ([ProductID])
GO
ALTER TABLE [dbo].[APA_QuestionProduct] CHECK CONSTRAINT [FK_APA_QuestionProduct_APA_Product]
GO
ALTER TABLE [dbo].[APA_QuestionProduct] WITH CHECK ADD CONSTRAINT [FK_APA_QuestionProduct_APA_Question] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[APA_Question] ([QuestionID])
GO
ALTER TABLE [dbo].[APA_QuestionProduct] CHECK CONSTRAINT [FK_APA_QuestionProduct_APA_Question]
GO
so the table behind has only 2 foreign keys (that are also primary keys). My question object has a list of products. When I update the ProductID foreign key and call 'SaveChanges' on the context no query/update is generated in db:
question.Products[1].ProductID = 4;
db.Entry(question.Products[1]).State = EntityState.Modified;
db.SaveChanges();
I looked in InteliTrace to check the query but no query is called over my QuestionProduct table even if my QuestionProduct object is changed. Why the table is not updated? No error is thrown.
Your entity QuestionProduct only contains key properties, no other scalar properties. Entity Framework does not allow to change (primary) key properties.
You must delete the old link record and create a new one to establish the new relationship, for example like so:
QuestionProduct oldProduct = question.Products[1];
QuestionProduct newProduct = new QuestionProduct
{
QuestionID = question.QuestionID,
ProductID = 4
};
db.QuestionProducts.Attach(oldProduct);
db.QuestionProducts.Remove(oldProduct);
db.QuestionProducts.Add(newProduct);
db.SaveChanges();
But you should really model this as a many-to-many relationship. You don't need the QuestionProduct entity and could have collections directly refering from Question to Product and vice versa without having to navigate through an intermediate entity.
It is shown here for example how it works.

Elegant linq solution for left joins with unique data

I woud like to inquire if my Linq solution below is a good solution or if there is a better way. I am new to using Linq, and am most familiar with MySQL. So I've been converting one of my past projects from PHP to .NET MVC and am trying to learn Linq. I would like to find out if there is a better solution than the one I came up with.
I have the following table structures:
CREATE TABLE maplocations (
ID int NOT NULL AUTO_INCREMENT,
name varchar(35) NOT NULL,
Lat double NOT NULL,
Lng double NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY name (name)
);
CREATE TABLE reservations (
ID INT NOT NULL AUTO_INCREMENT,
loc_ID INT NOT NULL,
resDate DATE NOT NULL,
user_ID INT NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY one_per (loc_ID, resDate),
FOREIGN KEY (user_ID) REFERENCES Users (ID),
FOREIGN KEY (loc_ID) REFERENCES MapLocations (ID)
);
CREATE TABLE Users (
ID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
pass VARCHAR(128) NOT NULL,
salt VARCHAR(5) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY unique_names (name),
UNIQUE KEY unique_email (email)
);
In MySQL, I use the following query to get the ealiest reservation at each maplocation with a non null date for any locations that don't have a reservation.
SELECT locs.*, if(res.resDate,res.resDate,'0001-01-01') as resDate, res.Name as User
FROM MapLocations locs
LEFT JOIN (
SELECT loc_ID, resDate, Name
FROM Reservations, Users
WHERE resDate >= Date(Now())
AND user_ID = Users.ID
ORDER BY resDate
) res on locs.ID = res.loc_ID
group by locs.ID
ORDER BY locs.Name;
In Linq, with Visual studio automatically creating much of the structure after connecting to the database, I have come up with the following equivalent to that SQL Query
var resList = (from res in Reservations
where res.ResDate >= DateTime.Today
select res);
var locAndRes =
(from loc in Maplocations
join res in resList on loc.ID equals res.Loc_ID into join1
from res2 in join1.DefaultIfEmpty()
join usr in Users on res2.User_ID equals usr.ID into join2
from usr2 in join2.DefaultIfEmpty()
orderby loc.ID,res2.ResDate
select new {
ID = (int)loc.ID,
Name = (string)loc.Name,
Lat = (double)loc.Lat,
Lng = (double)loc.Lng,
resDate = res2 != null ?(DateTime)res2.ResDate : DateTime.MinValue,
user = usr2 != null ? usr2.Name : null
}).GroupBy(a => a.ID).Select(b => b.FirstOrDefault());
So, I'm wondering is there a better way to perform this query?
Are these equivalent?
Are there any good practices I should be following?
Also, one more question, I'm having trouble getting this from the var to a List. doing something like this doesn't work
List<locAndResModel> locList = locAndRes.AsQueryable().ToList<locAndResModel>();
In the above snippet locAndResModel is just a class which has variables to match the int, string, double double, DateTime, string results of the query. Is there an easy way to get a list without having to do a foreach and passing the results to a constructor override? Or should I just add it to ViewData and return the View?
You'll want to take advantage of the automatic joins performed by the Entity Framework. Give this a try and let me know if it does what you want:
var locAndRes = from maplocation in MapLocations
let earliestReservationDate = maplocation.Reservations.Min(res => res.resDate)
let earliestReservation = (from reservation in mapLocation.Reservations
where reservation.resDate == earliestReservationDate && reservation.resDate >= DateTime.Today
select reservation).FirstOrDefault()
select new locAndResModel( maplocation.ID, maplocation.name, maplocation.Lat, maplocation.Lng, earliestReservation != null ? earliestReservation.resDate : DateTime.MinValue, earliestReservation != null ?earliestReservation.User.name : null)

MVC Entity Framework Model not returning correct data

Run into a strange problem while writing an ASP.NET MVC site. I have a view in my SQL Server database that returns a few date ranges. The view works fine when running the query in SSMS.
When the view data is returned by the Entity Framework Model, It returns the correct number of rows but some of the rows are duplicated.
Here is an example of what I have done:
SQL Server code:
EDITED: (table A)
CREATE TABLE [dbo].[A](
[ID] [int] NOT NULL,
[PhID] [int] NULL,
[FromDate] [datetime] NOT NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED
( [ID] ASC,
[FromDate] ASC
)) ON [PRIMARY]
CREATE TABLE [dbo].[B](
[PhID] [int] NOT NULL,
[FromDate] [datetime] NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_B] PRIMARY KEY CLUSTERED
( [PhID] ASC )) ON [PRIMARY]
go
CREATE VIEW C as
SELECT A.ID,
CASE WHEN A.PhID IS NULL THEN A.FromDate ELSE B.FromDate END AS FromDate,
CASE WHEN A.PhID IS NULL THEN A.ToDate ELSE B.ToDate END AS ToDate
FROM A
LEFT OUTER JOIN B ON A.PhID = B.PhID
go
INSERT INTO B (PhID, FromDate, ToDate) VALUES (100, '20100615', '20100715')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (1, NULL, '20100101', '20100201')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (1, 100, '20100615', '20100715')
INSERT INTO B (PhID, FromDate, ToDate) VALUES (101, '20101201', '20101231')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (2, NULL, '20100801', '20100901')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (2, 101, '20101201', '20101231')
So now, if you select all from C, you get 4 separate date ranges
In the Entity Framework Model (which I call 'Core'), the view 'C' is added.
in MVC Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
CoreEntities db = new CoreEntities();
var clist = from c in db.C
select c;
return View(clist.ToList());
}
}
in MVC View:
#model List<RM.Models.C>
#{
foreach (RM.Models.C c in Model)
{
#String.Format("{0:dd-MMM-yyyy}", c.FromDate)
<span>-</span>
#String.Format("{0:dd-MMM-yyyy}", c.ToDate)
<br />
}
}
When I run all this, it outputs this:
01-Jan-2010 - 01-Feb-2010
01-Jan-2010 - 01-Feb-2010
01-Aug-2010 - 01-Sep-2010
01-Aug-2010 - 01-Sep-2010
When it should do this (this is what the view returns):
01-Jan-2010 - 01-Feb-2010
15-Jun-2010 - 15-Jul-2010
01-Aug-2010 - 01-Sep-2010
01-Dec-2010 - 31-Dec-2010
Also, I've run the SQL profiler over it and according to that, the query being executed is:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[FromDate] AS [FromDate],
[Extent1].[ToDate] AS [ToDate]
FROM (SELECT
[C].[ID] AS [ID],
[C].[FromDate] AS [FromDate],
[C].[ToDate] AS [ToDate]
FROM [dbo].[C] AS [C]) AS [Extent1]
Which returns the correct data
So it seems that the entity framework is doing something to the data in the meantime.
To me, everything looks fine! Have I missed something?
Cheers,
Ben
EDIT:
sorry, table A should be:
CREATE TABLE [dbo].[A](
[ID] [int] NOT NULL,
[PhID] [int] NULL,
[FromDate] [datetime] NOT NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED
( [ID] ASC,
[FromDate] ASC
)) ON [PRIMARY]
I figured it out myself.
The problem was with the way the view was mapped in the entity model.
When it was added, it made the entity key just the ID. I needed it over the ID and FromDate. So I included the FromDate in the entity key and it works fine.

Resources