I have a entity framework database first set up and I'm having issues getting data from a table EF treats as an association because it's basically a navigation property. I have a Survey table with an EventId(PK), FacilityId, ExitDate, and Status. I also have a SurveyCategories table with CategoryID(PK), Description and a SurvCat table that just has SurveyId and CategoryId as foreign keys. I can get data from other related tables that don't use a middle table like SurvCat, but even following the documentation from the breeze site for navigation properties I cannot get anything loaded into the SurveyCategories array in each Survey object. I checked the metadata and it's showing the navigation property but I get nothing with this code:
var query = EntityQuery.from('Surveys')
.where("facilityId", "eq", whereClause)
.skip(currentPage * 5).take(5)
.expand("Facility")
.expand("SurveyCategories")
.expand("SurveyCite")
.expand("SurveyDL")
.orderBy(orderBy.survey)
.inlineCount(true);
Any help or links would be greatly appreciated.
I think that the answer for this other question can help you tu solve the problem: Error Loading related entities on demand (entityAspect.loadNavigationProperty()).
The N to N relations are not supported in breeze, so you have to use a intermediate entity to do work this.
Related
My question is pretty simple. I am working on Kentico 9 with its SQL Server database which contains several tables which had been added directly from the SQL Management Studio by an external contractor. The fact is that those tables are being used to store custom content which will be displayed for a site, but, in the code they don't have the code for making queries. I mean, they don't have Info and Provider classes.
https://docs.kentico.com/display/K82/Retrieving+database+data+using+ObjectQuery+API
According with this, all tables into the Kentico database can be accessed by invoking methods on these classes, but I don't have it this time.
Something like this, it will not work if I use my table name:
var user = UserInfoProvider.GetUserInfo("administrator");
var items = CustomTableItemProvider.GetItems("MyTable")
.TopN(10)
.WhereEquals("ItemCreatedBy", user.UserID)
.OrderBy("ItemCreatedWhen");
My question is:
can I query any table by its name?
One last thing:
I cannot declared those table as "custom table" because it seems to be a bug in the CMS.
Or you can pull data using your own SQL query:
var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);
Nevertheless I would recommend to create a custom class inside a custom module (much more robust than custom tables) instead and use the generated Info and InfoProvider classes to get and manipulate data.
I think an object has to be registered within the system (created through Kentico UI or API) in order to be pulled from DB with object query.
So I'd choose one of the following options:
Use Entity Framework or something similar to work with that data
Create appropriate custom tables or even custom module and push data there. Not sure why you can't create a custom table... What is an error you're getting?
If you need to present data on the UI only (without processing on the back end) - use just custom queries
Hope this helps.
If you are accessing in code then you could do it the good old fashioned way. If you want to pull data from the database to display on the website you could also do so by creating a custom query and using a transformation to display the fields, then use a repeater on the page to display the transformed data. Alternatively you can use a SQL datasource with a basic repeater, but you still have to create a transformation to display the data. Both methods allow you to access the data in the tables from within the CMS UI, no need to touch any code behind.
If your objective is to read data from these database tables to transform on webpage e.g. using CMS Repeater webpart, you can simply create custom query(s) in Kentico itself and load data using it. You can find the detail here on how to create custom custom queries and load data using it.
On the other hand you can also write your custom classes and define the custom methods where you can pull data using your own SQL query like this:
var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);
Lastly I don't think there should be any issue to create custom table instead of those direct DB tables, only thing we have to ensure code name of custom table should be unique means don't try to use exact same name because it'll cause exception due to same table name already exist in DB. You can please share exception you getting while creating custom table so that I can help you out further.
I have some points on a map with associated informations contained by Core Data, to link the points on the map with the associated info, I would like to have an ID for each point, so in my entity, I would need some ID property, I have read that Core Data has it's own IDs for every managed object but I'm wondering wether or not it would be a good approach for me to directly use them or if I should create my own ID system ?
If you think I should create my OWN ID system, how would I do that ?
Thank you.
CoreData is not an relational database and you should avoid thinking about own ID’s. You may need them only for syncing purposes with external databases. For more precise answer, you should write how your model looks.
[edited after comment]
I don't see that you need any relations. Let's sat you have MapPoint entity with lat, and long properties. If there is only one user note, you just add another property to it like notes. If you have many informations (many records) stored with one MapPoint you need to add Notes entity with properties note and mappoint and make a relation between them. When you insert new Notes object into CoreData you set mappoint property to already existing MapPoint object (fetched after user tap).
I would like to do the same as described in this post:
Entity Framework 4.1 dynamically retrieve summary for scalar properties from database table column description
I noticed that the user stated that he is using Entity Framework 4.1.
Can anyone confirm whether this feature is part of Entity Framework 5 or 6?
If not, do anybody know of someone that might have added this feature in EF 6, since it is open source now?
Summary
I have added descriptions to the columns and tables in MS SQL Designer. Instead of having to retype these as xml comments for my classes and properties in the generated Entity Framework classes, I would like to have these xml comments generated from the SQL descriptions.
Would something like this not be possible with a T4 template?
My Entity Framework knowledge is very lacking, so if someone can even just point me in the right direction, I am willing to figure this out on my own.
Any advice would be greatly appreciated!
Okay, I have figured out an alternative way.
It's not the answer to the original question, but it will work just as well.
What I have done was entered all the Descriptions from my MS SQL database into the Entity Models Documentation.Summary and Documentation.Long Description.
Then I looked at some code from these two posts:
Example1
Example2
and altered it slightly to work like this:
In the "YourEFModelName".tt file (NOT "YourEFModelName".Context.tt),
I looked for this line:
<#=codeStringGenerator.EntityClassOpening(entity)#>
to find the class header.
Then I added this right in front of it:
<#if (!ReferenceEquals(entity.Documentation, null))
{
#>
/// <summary>
/// <#=entity.Documentation.Summary#>
<#if (!ReferenceEquals(entity.Documentation.LongDescription, null) && !ReferenceEquals(entity.Documentation.LongDescription, ""))
{
#>
/// <#=entity.Documentation.LongDescription#>
<#}#>
/// </summary>
<#}#>
Note that the argument to the "EntityClassOpening" method is "entity", as used in the code you insert into the tt file.
You can use this code for the properties also. To find the properties, just look a bit lower down in the code for this line:
<#=codeStringGenerator.Property(edmProperty)#>
It will generate all your class properties. You can alter the code we used for the class comments to have "edmProperty" (the parameter to the "Property" method in the previous line) instead of "entity".
Then do the same for the Complex Properties and Navigation Properties found just below the edmProperties.
Once I added all my comments into the Entity Framework model and made these changes to the tt file, I right clicked the tt file and chose "run custom tool".
All my generated classes had xml comments with the descriptions I entered in the documentation property of all the classes and properties in my model.
My thanx goes to the people that contributed to the two example threads!
I am trying to create an association between 2 linq to sql entities, say Entity A and Entity B.
A uses a non-column attribute property ( named BaseDocumentType ) and a column attribute in an Association for "ThisKey" and 2 column attributes for "OtherKey". The following is an example of my Association attribute definition...
[System.Data.Linq.Mapping.AssociationAttribute ( ... ThisKey = "BaseDocumentType, Column2" , OtherKey = "Column1,Column2" )]
When I run it I get the following error...
"Data member 'System.String BaseDocumentType' of type 'Library' is not part of the mapping for type 'A'. Is the member above the root of an inheritance hierarchy?"
How can I define the relationship using the non-column attribute property or how do I make this work?
Thanks.
The message is quite clear. LINQ to SQL translates statements to SQL and you tried to use a property that doesn't map to a column, so it can't be translated to SQL.
You'll have to retrieve the entities you want from the database then try to query them using LINQ to Objects, ie LINQ operations on the resulting lists or arrays. A better option is to rethink your design and find a way to retrieve only the data you need from the database and avoid processing the results on the client.
Linq to SQL in this case prevented you from doing something really dangerous. It could have retrieved all the data and process them using your non-column attribute but that would create an enormous performance hit. Some naive LINQ providers actually do just that. Imagine retrieving 1000 objects from the database only to find the two objects that match over this non-column attribute.
Has anyone been able to use a SubSonic generated collection as a "business object datasource" with Microsoft report (rdlc)? I have generated the SubSonic class code but for some reason the report datasource window is not seeing the class as a potential object collection datasource.
Is there something I need to do for this to work?
Thanks in advance...vsdotnetguy
I have loaded Reporting Service reports from business objects before (loaded via NHibernate -- which isn't exact but close enough for argument sake).
Couple of key points:
1. return your objects in List, even if you are only returning one object.
2. You want FLAT business objects. You might have to go thru a DTO transformation to get that. By flat, I mean the most complex property you can have in a business object is a string and a number (int, decimal, double). If you are expecting to grab a value like this:
myObject.Customer.Name, forget it. Create a CustomerName property.
3. If you need data from multiple places try to break up your reports into subreports. You key off of the datasource key to figure out what data to return to the report.
I'll add more as I remember, it has been a few months since I've done this.
Yes I've done it, you should only need to make sure the project containing your reports references your SubSonic project (obviously :).
Sometimes I've also found that Visual Studio can get a little borked and require a restart before repopulating the datasource window with SubSonic generated objects.
Thx Chris and Adam,
Here is the answer I found.
In my case I wanted to dynamically set the main and subreport datasources at run time using the SubSonic object collections. However, I also wanted to design the report layout using drag and drop of the datasource columns.
But I was unable to design the report using drag&drop because none of my SubSonic collections were showing up in the Website Data Sources.
However, later while I was doing some control binding using the ObjectDataSource control, I noticed that NOW my SubSonic collections were showing up in the Website DataSources window and I could drag and drop the report layout.
So if you are dynamically setting the report datasources at run time and ARE NOT using the ObjectDataSource control already in your project, you MUST add a dummy ObjectDataSource control to one of your aspx pages. This will then make the business object datasources show up in the report designer.