I'm trying to join two tables from two different catalogs but I'm not able to get to work.
I know I have to do something with CatalogNameOVerwriteHashtable but apparently I'm doing something wrong.
The documentation link (http://www.llblgen.com/documentation/2.6/using%20the%20generated%20code/Adapter/gencode_dataaccessadapter_adapter.htm) doesn't give enough information to resolve my challenge.
I have the following situation:
I've got two Catalogs: CatalogA and CatalogB
There's Article-table in CatalogA and a StockCount-table in CatalogB
I've created a manual relation. So far so good.
My guess is that I have the following actions:
Create a new CatalogNameOverwriteHashtable instance:
var foo = new CatalogNameOverwriteHashtable();
foo.Add("StockCount", "CatalogA");
foo.Add("Article", "CatalogB");
Assign it to the adapter: adapter.CatalogNameOverwrites = foo;
Which results in the following query:
SELECT
[dbo].[StockCount].[ArticleId],
[dbo].[Article].[Description],
[dbo].[StockCount].[ShopId],
[dbo].[StockCount].[LastMutationDateTime]
FROM ( [dbo].[StockCount] INNER JOIN
[dbo].[Article] ON [dbo].[StockCount].[ArticleId]=[dbo].[Article].[ArticleId])
Clearly I'm doing something wrong because the catalog name is missing in the query. The question is, what?
In the end the solution is very simple and proves why LLBLGen is so powerful and easy to use.
First of all. When you generate your entities the catalog name is stored in FieldInfoProviders. This can be retrieved with following instruction:
var catalogName = ORMapper.Utils.DatabasePersistenceInfo.GetFieldPersistenceInfo(ArticleFields.PKey).SourceCatalogName;
This catalog name is the catalog you used to generate the entities against. This is probably your development database.
Next thing you do is map the development catalog name with the production catalog name. Like so:
adapter = new DataAccessAdapter("Your connection string");
adapter.CatalogNameOverwrites =new CatalogNameOverwriteHashtable();
adapter.CatalogNameOverwrites.Add(catalogName, "ProductionCatalogName");
Related
I have an issue with next scheme, I attached it.I want to query from my database with only one object with "Manufacturer" class. Like:
var res = new XPQuery<Manufacturer>(session);
And then query all info that are related to my condition in LINQ.
I have tried XPLiteObject, XPObject, Association attribute, NoForeignKey Attribute, XPOCollection and a lot of stuff but nothing didn't help me.
I have tried a lot of approaches and every time I have new exception like:
SelectMany - method is not supported.
Can't set foreign key in table.
Duplicate primary key.
My question is: how to describe classes for normal extraction data from db?
UPD:
My solution now is: to use .ToList() at every object
and then use linq-query for join data and make needed query.
var manufacturer = new XPQuery<Manufacturer>(session).ToList();
var cars = new XPQuery<Car>(session).ToList();
var countries = new XPQuery<Country>(session).ToList();
var result = from m in manufacturer ....
So, I have found a solution to my question.
I downloaded DevExpress that can add templates for visual studio.
Then I select Add new item to my project named "DevExpress ORM DataModel Wizard".
This wizard can create persistent objects for existing database.
After that I can query database with next syntax:
var manufacturer = new XPQuery<Manufacturer>(session).Select(x => x....)...;
But if you want to use .SelectMany() in your LINQ query you should use .ToList() and then use .SelectMany(). I faced with a lot of issues when I have tried to join or perform some other LINQ related operations. Well, if you got some errors, firstly after .Select() try .ToList() and then perform your operation.
I'm working on an ASP.Net application which uses a SQL-Server DB and database entities.
Further i got three database entities which are dependend on each other.
This is the dependency hierarchy:
Instance (Key: InstanceID)
CustomField (Key: CustomFieldID, InstanceID)
CustomFieldData (Keys: CustomFieldDataID, CustomFieldID)
CustomFieldData_Person (Keys: CustomFieldData_PersonID, CustomFieldDataID)
I can find out the entries from the entity CustomField by this with the InstanceID:
var customFieldEntries = DB_Instance_Singleton.getInstance.CustomField.Where(x => x.InstanceID == instanceId);
Now i want to find out all entries from CustomFieldData_Person which belong to the hierarchy with the InstanceID as key.
In SQL i would write something like this:
SELECT * FROM CustomFieldData_Person WHERE CustomFieldDataID in (
SELECT * FROM CustomFieldData WHERE CustomFieldID in (
SELECT * FROM CustomField WHERE InstanceID = instanceId))
Unfortunately i'm absolutely new to LINQ.
So my question is, how can i write such a nested query in LINQ (aacording to the first code example above)?
Thanks in advance!
Firstly if you create your ER model correctly you will have most of that logic already set up for you
Person would have a property Person.CustomData which would have Properties for Field and Value so you can just navigate the object structure
however if you dont have that then you can just convert the in statements to Contains
CustomFieldData_Person.Where(cfdp=>CustomFieldData.Where(nested query for CustomFieldData).Contains(cfdp.CustomFieldDataID )
I think this link could be a good starting point for your question. Anyway take a look at Pranav's comment, it points to a helpful question
I'm porting some existing (dynamic) SQL to C# via the SMO namespace. I'm having a little trouble figuring out how to join an existing database to my AlwaysOn Availability Group, though. The SMO namespace has a Database object and an AvailabilityDatabase object, but the two seem to be somewhat orthogonal...I can't see a way to move back and forth between the two concepts. In our existing implementation we create the database, perform some operations on it, create a full backup and then join it to the Availability Group. I'm trying to recreate this workflow in SMO, but getting hung up at the join to Availability Group step. If I do this...
AvailabilityGroup ag = new AvailabilityGroup(sqlServer, myExistingAgName);
AvailabilityDatabase agDb = new AvailabilityDatabase(ag, myExistingDbName);
agDb.JoinAvailablityGroup();
The operation fails and tells me that the AvailabilityDatabase hasn't been created yet. However, if I do this...
AvailabilityGroup ag = new AvailabilityGroup(sqlServer, myExistingAgName);
AvailabilityDatabase agDb = new AvailabilityDatabase(ag, myExistingDbName);
agDb.Create();
agDb.JoinAvailablityGroup();
The operation fails and tells me that the creation of the AvailabilityDatabase failed. Although the error message doesn't explicitly state this, I would assume the reason for the failure was that a DB by the name of myExistingDbName already exists, which is expected. I'm sure I'm just missing something fundamental here, but the MSDN documentation isn't very illustrative, and I'm not having any luck finding any tutorials/examples of this sort of thing online.
To add the database to the Availability Group on primary, I used the following...
// note that the Availability Group is instantiated differently than above
AvailabilityGroup ag = sqlServer.AvailabilityGroups[availabilityGroup];
AvailabilityDatabase agDb = new AvailabilityDatabase(ag, database);
agDb.Create();
After restoring the database on the secondary server(s), here's what I used to join it to the Availability Group...
AvailabilityGroup ag = sqlServer.AvailabilityGroups[availabilityGroup];
ag.AvailabilityDatabases[database].JoinAvailablityGroup();
I’d like to consult about a problem I have faced. I've started working on a project with a very difficult database: many tables in the DB don’t have primary keys or have multiple PKs, so I can't add correct associations for all entities in my edmx. However, for some entities it’s possible and I managed to do so. Thus, I have two entities with an association between them: Vulner and VulnerDescription. And I have a "bad" connection table for Vulners called VulnerObjectTie (with a mental FK: VulnerObjectTie.Vulner = Vulner.Id), which I can’t add correct associations to. So, I decided to do add the following LinqtoEntities query:
var vulerIdList = from vulner in _businessModel.DataModel.VulverSet.Include("Descriptions")
join objectVulnerTie in _businessModel.DataModel.ObjectVulnerTieSet on vulner.Id equals objectVulnerTie.Vulner
where softwareId.Contains(objectVulnerTie.SecurityObject)
select vulner;
where description is Navigation Property for an association with the VulnerDescription table. The query works, but it does not load the Descriptions property. However, if I remove the join operator, then Descriptions are loaded correctly.
The most obvious solution for this problem is to divide one query into the next two queries:
var vulerIdList = from vulner in _businessModel.DataModel.VulverSet
join objectVulnerTie in _businessModel.DataModel.ObjectVulnerTieSet on vulner.Id equals objectVulnerTie.Vulner
where softwareId.Contains(objectVulnerTie.SecurityObject)
select vulner.Id;
var query = from vulner in _businessModel.DataModel.VulverSet.Include("Descriptions")
where vulerIdList.Contains(vulner.Id)
select vulner;
But I think it looks ugly. Can anyone suggest a more simple solution for this problem, or is it just a special feature of EF4??
thankyouplease :))
It's a known 'feature' or limitation, depending on how you look at it. Here's an interesting discussion on the topic, I'm sure there are more references to find: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d700becd-fb4e-40cd-a334-9b129344edc9/
The problem here is that EF is not very well suited for "bad databases". EF (and especially all automation tools like model wizard) expects clear and correct database design.
Include is not supported in queries using custom joins or projections. Not supported in this case means that it is completely omitted.
I'm sure this is straight forward but I'm very new to entity queries and has probably been asked before.
What i need to to search for all business in my database where they have a category that exists in a collection of categories I have built up
IList<businessCategory> busCatList;
busCatList.Add(businessCategory.CreatebusinessCategory(1,"Tourism"));
busCatList.Add(businessCategory.CreatebusinessCategory(2,"Accomidation"));
busCatList.Add(businessCategory.CreatebusinessCategory(3,"Entertainment"));
busCatList.Add(businessCategory.CreatebusinessCategory(4,"Bar"));
busCatList.Add(businessCategory.CreatebusinessCategory(5,"Club"));
var items = Data.DBEntities.business.Where(b.businessCategory.Contains(busCatList) );
I know the syntax of the query is wrong but essentially what i what the query to do it pull out all the business where it has a category matching any of the categories in the busCatLsit
In my database one business can be linked to many categories
In SQL I would do
SELECT name FROM business
join businessCategoryRlnshp on businessCategoryRlnshp.businessID = business.ID
where categoryID in (1,2,3)
just trying to read your mind here ;)
var items = busCatList.Where(businessCategory => b.businessCategory.Contains(businessCategory));
I can't really imagine a nice solution in linq - I mean other than some kind of performing the query several times - one for each business category.
However in SQL Server 2008 there is a new feature - passing a table variable to stored procedure. This can be done from code by passing a DataSet with 1 DataTable as a parameter.
You can of course write an extension method for IEnumerable to convert it to a DataSet similar to ToList() or ToDictionary() methods.
Stored procedure returning entities can be used in EntityFramework 1.0 so this should theoretically make the puzzle click.
PS> There's also a solution using E-SQL and probably query builder methods.