Linq To Sql Entity Updated from Trigger - sql-server

I have a Table called Address. I have a Trigger for insert on that table that does some spacial calculations on the address that determines what neighborhood boundaries it is in.
address = new Address
{
Street = this.Street,
City = this.City,
State = this.State,
ZipCode = this.ZipCode,
latitude = this.Latitude,
longitude = this.Longitude,
YearBuilt = this.YearBuilt,
LotSize = this.LotSize,
FinishedSize = this.FinishedSize,
Bedrooms = this.Bedrooms,
Bathrooms = this.Bathrooms,
UseCode = this.UseCode,
HOA = this.HOA,
UpdateDate = DateTime.Now
};
db.AddToAddresses(address);
db.SaveChanges();
In the database i can clearly see that the Trigger ran and updated the neighborhoodID in the address table for the row. I tried to just reload that record to get the assigned id like this:
address = (from a in db.Addresses where a.AddressID == address.AddressID select a).First();
In the debugger i can clearly see that the address.AddressID is correct, entity doesn't update in memory.
Is there any work around for this?

I added
db.Refresh(System.Data.Objects.RefreshMode.StoreWins, address);
right before
address = (from a in db.Addresses where a.AddressID == address.AddressID select a).First();
and that fixed my problem.

Related

Select specific columns with repository in Entity Framework Core

I have a big table with a binary column for picture. I need to show contents of this table in a view and make it searchable. I have tried only selecting a subset of columns that are needed in this. However, the generated SQL always has all the columns of the table in the generated query.
public IQueryable<ApplicantDTO> GetApplicantQueryable()
{
return DataContext.Applicants
.Include(a => a.Nationality)
.Select(x => new ApplicantDTO
{
Id = x.Id,
BirthDate = x.BirthDate,
Gender = x.Gender,
FirstName = x.FirstName,
LastName = x.LastName,
OtherName = x.OtherName,
MobileNo = x.MobileNo,
Age = x.Age,
Nationality = x.Nationality.National,
Admitted = x.admitted,
Completed = x.Completed
})
.Where(a => a.Admitted == false && a.Completed == true)
.OrderBy(a => a.LastName)
.AsNoTracking();
}
But instead of just specifying the above rows, the generated SQL from profiler is
SELECT
[a].[Id], [a].[BirthDate], [a].[BirthPlace], [a].[CompleteDate],
[a].[Completed], [a].[ContentType], [a].[CreateDate],
[a].[Denomination], [a].[Disability], [a].[Email],
[a].[FirstName], [a].[Gender], [a].[HomeTown], [a].[LastName],
[a].[MarryStatus], [a].[MatureApp], [a].[MobileNo], [a].[NationalityID],
[a].[OtherName], [a].[Passport], [a].[Pin], [a].[PostalAddress],
[a].[Region], [a].[Religion], [a].[ResAddress], [a].[SerialNo],
[a].[Title], [a].[VoucherID], [a].[admitted], [a.Nationality].[National]
FROM
[Applicants] AS [a]
INNER JOIN
[Nationality] AS [a.Nationality] ON [a].[NationalityID] = [a.Nationality].[Id]
WHERE
([a].[admitted] = 0)
AND ([a].[Completed] = 1)
ORDER BY
[a].[LastName]
With all the underlying columns all included in the query.
I tried putting it in an anonymous type before casting it to the ApplicantDTO but still the same effect.
What's wrong?

Prestashop - how to set custom tax for products of one manufacturer?

My prestashop site has different manufacturers and I want to set 20% TAX/VAT for products of one manufacturer and don't effect the rest of manufacturers. Which way and how could I do it? Let's say the manufacturer identified by it's id - manufacturer_id = 1.
UPDATE ps_product_shop ps
LEFT JOIN
ps_product p ON ps.id_product = p.id_product
SET
ps.id_tax_rules_group = XXX
WHERE
p.id_manufacturer = 1;
replace XXX with actual tax rules id.
You need to repeat this update query for ps_product table to have consistent data.
must be online file upload on your website and call
require_once('config/defines.inc.php');
$id_tax_rules = XXXX; // ID TAX RULES GROUP
$id_manufacturer = 1; // ID MANUFACTURER
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'product_shop SET id_tax_rules_group = '.$id_tax_rules.' WHERE id_manufacturer = '.$id_manufacturer);
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'product SET id_tax_rules_group = '.$id_tax_rules.' WHERE id_manufacturer = '.$id_manufacturer);
?>
Regards

Returning empty venue data in FQL?

My problem is that it seems for a few events there is simply no venue data at all.. for example this event (code):
SELECT name, location, attending_count, venue, eid FROM event WHERE eid = '447067818737101'
Is it possible to search for events where no venue data exists, like addig this to the code and the result still appear?
SELECT name, location, attending_count, venue, eid FROM event WHERE eid = '447067818737101'
AND venue.city = NULL OR venue.city = ""
My goal is to select from many event results ONLY the ones WITHOUT venue.city data in the final code!
UPDATE:
Solution for those who may have problems with events where venue data is not available:
strlen(venue.city) = 0 , if no city in venue struct it is simply equal to zero length string, and this surely works on other missing venue elements.
I think the problem is with your use of AND and OR operator. You must know that AND operator has a precedence over OR operator. Therefore, your query will be executed as:
SELECT name, location, attending_count, venue, eid FROM event WHERE (eid = '447067818737101' AND venue.city = NULL) OR venue.city = ""
So instead, you can try:
SELECT name, location, attending_count, venue, eid FROM event WHERE eid = '447067818737101' AND venue.city IS NULL

Algorithm to Organize Array

Here's the problem:
I have an array that has information about users. Each entry is separated by a blank field in the array.
For this example let's say the data fields are ID, first, last, phone, and email. However, if a user doesn't have a value for a particular field, then it is omitted entirely.
So here is what the array looks like
users[0] = 45049345
users[1] = Bob
users[2] = Smith
users[3] = 789-456-1230
users[4] = bob#gmail.com
users[5] =
users[6] = 63515987
users[7] = Joe
users[6] = Schmoe
users[8] = joe#gmail.com
users[9] =
I want to loop this array and store the data for each user in a database, however I have no clue how to verify that I am storing the right information in the place because there is no pattern in the array. Since Joe doesn't have a phone number, his email is would be stored as his phone number. This would result in ever subsequent entry to be off by 1 index.
Any ideas on how to go about this?
P.S. I am using node.js
EDIT: here is an example of the actual data
021870143-14
lastName
firstName
U
5/16/1988
11/6/2008
A
11/6/2008 6:0:2
NF
245
MAIN ST.
101
NEW YORK
NY
10002
11/4/2008
34
SD1
MUNC1J-036
MAG1-1
LEG77
SENT34
CONG5
CNTY
34-1
10/27/2008 19:59:53
NF
Here's pseudo code because I don't know javascript. I'm basing this off the fact that I think Javascript has dictionaries/associative arrays/hash tables etc.
new_user = {}
for i from 0 to arr.length
if arr[i] = null/Omitted field/nil/None/whatever
database.add_entry(new_user) // Add finished user table to database
new_user = {} // Start new dictionary
else
field = arr[i]
if field contains '#'
new_user.email = field
else if field contains '-'
new_user.phone_number = field
else if is_a_number(field)
new_user.id = field
else if new_user has first_name
new_user.last_name = field
else
new_user.first_name = field
Use tokenization ..
example:
users[0] = "45049345,Bob,Smith,789-456-1230,bob#gmail.com";
users[1] = "63515987,Joe,Schmoe,-,joe#gmail.com";
Process:
for (user in users)
{
for (detail in user)
{
document.write (detail + "<br>");
}
}

What is the problem with the logic in my UPDATE statement?

I would appreciate some help with an UPDATE statement.
I want to update tblOrderHead with the content from tblCustomer where the intDocumentNo corresponds to the parameter #intDocumentNo. But when I run the my statement, the order table is only updated with the content from the first row of the customer table.
What is the problem with my logic?
I use Microsoft SQL Server.
Thanks,
Stefan
UPDATE dbo.tblOrderHead
SET dbo.tblOrderHead.intCustomerNo = #intCustomerNo ,
dbo.tblOrderHead.intPaymentCode = dbo.tblCustomer.intPaymentCode,
dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
dbo.tblOrderHead.txtRegionCode = dbo.tblCustomer.txtRegionCode,
dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM dbo.tblOrderHead
INNER JOIN dbo.tblCustomer ON dbo.tblOrderHead.intOrderNo = #intDocumentNo
Solution
If anyone as stupid as me out there thing the same thing, this is how you solve it:
UPDATE dbo.tblOrderHead
SET dbo.tblOrderHead.intCustomerNo = #intCustomerNo ,
dbo.tblOrderHead.intPaymentCode = dbo.tblCustomer.intPaymentCode,
dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
dbo.tblOrderHead.txtRegionCode = dbo.tblCustomer.txtRegionCode,
dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM dbo.tblOrderHead
INNER JOIN dbo.tblCustomer ON dbo.tblOrderHead.intCustomerNo = dbo.tblCustomer.intCustomerNo
AND dbo.tblOrderHead.intOrderNo = #intDocumentNo
Problem is that, you are not specifying the condition on which the 2 tables tblOrderHead, tblCustomer need to be joined!
you need something like
INNER JOIN dbo.tblCustomer
ON dbo.tblOrderHead.someColumn = dbo.tblCustomer.someColumn
and dbo.tblOrderHead.intOrderNo = #intDocumentNo

Resources