How to Map Sql Query to EF Models when using FromSql
I am trying to use .FromSql but I keep getting an exception
"Required Column Id is Missing"
Now all my Models have Id so I have no clue which one I am missing, or if I am writing my "As"
statements wrong and it is not mapping properly.
var inventoryItems = dbContext.InventoryItems.AsNoTracking().FromSql(@"SELECT Brands.Id AS BrandsId, InventoryItems.Id AS InventoryItemsId,
Companies.Id AS CompaniesIds, Countries.Id AS CountriesId, States.Id AS StatesId, Branches.Id AS BranchesId,
Branches.CountryId, Branches.StateId, States.CountryId AS StatesCountryId, InventoryItems.InventoryCategoryId, InventoryItems.BrandId, InventoryItems.BranchId, Branches.CompanyId
FROM Branches INNER JOIN
Brands ON Branches.Id = Brands.Id INNER JOIN
Companies ON Branches.CompanyId = Companies.Id INNER JOIN
Countries ON Branches.CountryId = Countries.Id INNER JOIN
InventoryItems ON Branches.Id = InventoryItems.BranchId AND Brands.Id = InventoryItems.BrandId INNER JOIN
States ON Branches.StateId = States.Id AND Countries.Id = States.CountryId ).ToList();
Model example and all my models follow the same pattern
public class InventoryItem
{
public int Id { get; set; } //PK name
public int InventoryCategoryId { get; set; } // FK name pattern
public int BranchId { get; set; }
public virtual Branch Branch { get; set; }
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
}
I am using FromSql because I need to do a where clause(not shown) that filters on a json column with EF core does not support.
sql-server entity-framework ef-core-2.0
add a comment |
I am trying to use .FromSql but I keep getting an exception
"Required Column Id is Missing"
Now all my Models have Id so I have no clue which one I am missing, or if I am writing my "As"
statements wrong and it is not mapping properly.
var inventoryItems = dbContext.InventoryItems.AsNoTracking().FromSql(@"SELECT Brands.Id AS BrandsId, InventoryItems.Id AS InventoryItemsId,
Companies.Id AS CompaniesIds, Countries.Id AS CountriesId, States.Id AS StatesId, Branches.Id AS BranchesId,
Branches.CountryId, Branches.StateId, States.CountryId AS StatesCountryId, InventoryItems.InventoryCategoryId, InventoryItems.BrandId, InventoryItems.BranchId, Branches.CompanyId
FROM Branches INNER JOIN
Brands ON Branches.Id = Brands.Id INNER JOIN
Companies ON Branches.CompanyId = Companies.Id INNER JOIN
Countries ON Branches.CountryId = Countries.Id INNER JOIN
InventoryItems ON Branches.Id = InventoryItems.BranchId AND Brands.Id = InventoryItems.BrandId INNER JOIN
States ON Branches.StateId = States.Id AND Countries.Id = States.CountryId ).ToList();
Model example and all my models follow the same pattern
public class InventoryItem
{
public int Id { get; set; } //PK name
public int InventoryCategoryId { get; set; } // FK name pattern
public int BranchId { get; set; }
public virtual Branch Branch { get; set; }
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
}
I am using FromSql because I need to do a where clause(not shown) that filters on a json column with EF core does not support.
sql-server entity-framework ef-core-2.0
add a comment |
I am trying to use .FromSql but I keep getting an exception
"Required Column Id is Missing"
Now all my Models have Id so I have no clue which one I am missing, or if I am writing my "As"
statements wrong and it is not mapping properly.
var inventoryItems = dbContext.InventoryItems.AsNoTracking().FromSql(@"SELECT Brands.Id AS BrandsId, InventoryItems.Id AS InventoryItemsId,
Companies.Id AS CompaniesIds, Countries.Id AS CountriesId, States.Id AS StatesId, Branches.Id AS BranchesId,
Branches.CountryId, Branches.StateId, States.CountryId AS StatesCountryId, InventoryItems.InventoryCategoryId, InventoryItems.BrandId, InventoryItems.BranchId, Branches.CompanyId
FROM Branches INNER JOIN
Brands ON Branches.Id = Brands.Id INNER JOIN
Companies ON Branches.CompanyId = Companies.Id INNER JOIN
Countries ON Branches.CountryId = Countries.Id INNER JOIN
InventoryItems ON Branches.Id = InventoryItems.BranchId AND Brands.Id = InventoryItems.BrandId INNER JOIN
States ON Branches.StateId = States.Id AND Countries.Id = States.CountryId ).ToList();
Model example and all my models follow the same pattern
public class InventoryItem
{
public int Id { get; set; } //PK name
public int InventoryCategoryId { get; set; } // FK name pattern
public int BranchId { get; set; }
public virtual Branch Branch { get; set; }
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
}
I am using FromSql because I need to do a where clause(not shown) that filters on a json column with EF core does not support.
sql-server entity-framework ef-core-2.0
I am trying to use .FromSql but I keep getting an exception
"Required Column Id is Missing"
Now all my Models have Id so I have no clue which one I am missing, or if I am writing my "As"
statements wrong and it is not mapping properly.
var inventoryItems = dbContext.InventoryItems.AsNoTracking().FromSql(@"SELECT Brands.Id AS BrandsId, InventoryItems.Id AS InventoryItemsId,
Companies.Id AS CompaniesIds, Countries.Id AS CountriesId, States.Id AS StatesId, Branches.Id AS BranchesId,
Branches.CountryId, Branches.StateId, States.CountryId AS StatesCountryId, InventoryItems.InventoryCategoryId, InventoryItems.BrandId, InventoryItems.BranchId, Branches.CompanyId
FROM Branches INNER JOIN
Brands ON Branches.Id = Brands.Id INNER JOIN
Companies ON Branches.CompanyId = Companies.Id INNER JOIN
Countries ON Branches.CountryId = Countries.Id INNER JOIN
InventoryItems ON Branches.Id = InventoryItems.BranchId AND Brands.Id = InventoryItems.BrandId INNER JOIN
States ON Branches.StateId = States.Id AND Countries.Id = States.CountryId ).ToList();
Model example and all my models follow the same pattern
public class InventoryItem
{
public int Id { get; set; } //PK name
public int InventoryCategoryId { get; set; } // FK name pattern
public int BranchId { get; set; }
public virtual Branch Branch { get; set; }
public int BrandId { get; set; }
public virtual Brand Brand { get; set; }
}
I am using FromSql because I need to do a where clause(not shown) that filters on a json column with EF core does not support.
sql-server entity-framework ef-core-2.0
sql-server entity-framework ef-core-2.0
asked Nov 23 '18 at 23:26
chobo2chobo2
30.7k152404687
30.7k152404687
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
a .FromSql query returns a single Entity Type. That query should return Id, InventoryCategoryId, BranchId, BrandId,
one column for each property of Inventory Item (except for Navigation Properties).
If you want to load multiple different entity types, you can't use a single .FromSql() call. Instead use one of the methods detailed here. Especially note:
Entity Framework Core will automatically fix-up navigation properties
to any other entities that were previously loaded into the context
instance. So even if you don't explicitly include the data for a
navigation property, the property may still be populated if some or
all of the related entities were previously loaded.
So you can fetch the Entities you need across multiple seperate queries if you want. But this "fix-up" doesn't happen if you suppress change tracking on your Entities, as it's the change tracker that does this.
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453844%2fhow-to-map-sql-query-to-ef-models-when-using-fromsql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
a .FromSql query returns a single Entity Type. That query should return Id, InventoryCategoryId, BranchId, BrandId,
one column for each property of Inventory Item (except for Navigation Properties).
If you want to load multiple different entity types, you can't use a single .FromSql() call. Instead use one of the methods detailed here. Especially note:
Entity Framework Core will automatically fix-up navigation properties
to any other entities that were previously loaded into the context
instance. So even if you don't explicitly include the data for a
navigation property, the property may still be populated if some or
all of the related entities were previously loaded.
So you can fetch the Entities you need across multiple seperate queries if you want. But this "fix-up" doesn't happen if you suppress change tracking on your Entities, as it's the change tracker that does this.
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
add a comment |
a .FromSql query returns a single Entity Type. That query should return Id, InventoryCategoryId, BranchId, BrandId,
one column for each property of Inventory Item (except for Navigation Properties).
If you want to load multiple different entity types, you can't use a single .FromSql() call. Instead use one of the methods detailed here. Especially note:
Entity Framework Core will automatically fix-up navigation properties
to any other entities that were previously loaded into the context
instance. So even if you don't explicitly include the data for a
navigation property, the property may still be populated if some or
all of the related entities were previously loaded.
So you can fetch the Entities you need across multiple seperate queries if you want. But this "fix-up" doesn't happen if you suppress change tracking on your Entities, as it's the change tracker that does this.
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
add a comment |
a .FromSql query returns a single Entity Type. That query should return Id, InventoryCategoryId, BranchId, BrandId,
one column for each property of Inventory Item (except for Navigation Properties).
If you want to load multiple different entity types, you can't use a single .FromSql() call. Instead use one of the methods detailed here. Especially note:
Entity Framework Core will automatically fix-up navigation properties
to any other entities that were previously loaded into the context
instance. So even if you don't explicitly include the data for a
navigation property, the property may still be populated if some or
all of the related entities were previously loaded.
So you can fetch the Entities you need across multiple seperate queries if you want. But this "fix-up" doesn't happen if you suppress change tracking on your Entities, as it's the change tracker that does this.
a .FromSql query returns a single Entity Type. That query should return Id, InventoryCategoryId, BranchId, BrandId,
one column for each property of Inventory Item (except for Navigation Properties).
If you want to load multiple different entity types, you can't use a single .FromSql() call. Instead use one of the methods detailed here. Especially note:
Entity Framework Core will automatically fix-up navigation properties
to any other entities that were previously loaded into the context
instance. So even if you don't explicitly include the data for a
navigation property, the property may still be populated if some or
all of the related entities were previously loaded.
So you can fetch the Entities you need across multiple seperate queries if you want. But this "fix-up" doesn't happen if you suppress change tracking on your Entities, as it's the change tracker that does this.
edited Nov 24 '18 at 0:24
answered Nov 23 '18 at 23:56
David Browne - MicrosoftDavid Browne - Microsoft
15.7k2626
15.7k2626
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
add a comment |
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
So are you saying FromSql cannot handle other table columns then? Ie Eager loading? If so what should I use instead.
– chobo2
Nov 24 '18 at 0:01
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
Yep. See the edit.
– David Browne - Microsoft
Nov 24 '18 at 0:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453844%2fhow-to-map-sql-query-to-ef-models-when-using-fromsql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown