I'm trying to write a javascript fetch for an intersect entity (created by N:N relationship between 2 entities). I see C# samples everywhere but nothing for javascript. Is this possible?
Fetch is pretty universal between C# and JavaScript, so the same query should work in both.
http://blog.customereffective.com/blog/2011/05/execute-fetch-from-javascript-in-crm-2011.html
Related
According to Microsoft Docs
https://learn.microsoft.com/en-us/ef/core/modeling/relationships#other-relationship-patterns
Many-to-many relationships without an entity class to represent the
join table are not yet supported.
Ok, this leads to a nightmare when you need to migrate apps with several many-to-many relationships that were handled perfectly by EF5.
Now I have Keyword, Tag and KeywordTag entities set up as described in the link.
If I have a keyword entity, which is the correct syntax to retrieve all tags associated with such keyword?
In EF5 it was
var kwd = _context.Keywords.Find(my_kwd_id);
var tagList = kwd.Tags;
Which is the equivalent with EF Core?
Intellisense allows me to write
kwd.KeywordTags
but not
kwd.KeywordTags.Tags
...so I cannot find how to access Tags in any way...
Please don't tell me that i have to explicitly search then loop the KeywordTag entity to extract Tags ...
Since EF Core does not have exact parity with older versions of EF, you need to write bit different code. You would need to do what #Ivan suggested in comments. You also need to eager load your collection because lazy loading is not available. That means you need to do database query explicitly.
Method 1: Instead of using Find you query database directly and bring in related data. (kwd/tagList) would have same data as you are seeing in EF5.
var kwd = db.Keywords.Include(k => k.KeywordTags)
.ThenInclude(kt => kt.Tag)
.FirstOrDefault(k => k.Id == my_kwd_id);
var tagList = kwd.KeywordTags.Select(kt => kt.Tag).ToList();
Alternatively, you can use find but load navigations explicitly. This is somewhat similar to lazy loading but since it is not available you ask EF to load navigations. This will have really bad perf as you will send 1 query to fetch all entries from join table & then 1 query for each related tag in tags table. If you are interested in knowing the way to write it, I can post code for that too.
Tags is an IEnumerable<Tag>. You can iterate over Tags with a foreach loop.
I am currently working on a project where we are rewriting software that was originally written in Visual DataFlex and we are changing it to use SQL and rewriting it into a C# client program and a C#/ASP.Net website. The current database for this is really horrible and has just had columns added to table or pipe(|) characters stuck between the cell values when they needed to add new fields. So we have things like a person table with over 200 columns because stuff like 6 lots of (addressline1, addressline2, town, city, country, postcode) columns for storing different addresses (home/postal/accountPostal/ect...).
What we would like to do is restructure the database, but we also need to keep using the current structure so that the original software can still work as well. What I would like to know is would it be possible using Linq to write a DataContext Object Model Class that could sort of interpret the data base structures so that we could continue to use the current database structure, but to the code it could look like we where using the new structure, and then once different modules of the software are rewritten we could change the object model to use the correct data structure???
First of all, since you mention the DataContext I think you're looking at Linq to SQL? I would advice to use the Entity Framework. The Entity Framework has more advanced modeling capabilities that you can use in a scenario as yours. It has the ability to construct for example a type from multiple tables, use inheritance or complex types.
The Entity Framework creates a model for you that consists of three parts.
SSDL which stores how your database looks.
CSDL which stores your model (your objects and the relationships between them)
MSL which tells the Entity Framework how to map from your objects to the database structure.
Using this you can have a legacy database and map this to a Domain Model that's more suited to your needs.
The Entity Framework has the ability to create a starting model from your database (where all tables, columns and associations are mapped) en then you can begin restructuring this model.
These classes are generated as partial so you could extend them by for exampling splitting the database piped fields into separate properties.
Have you also thought about using Views? If possible you could at views to your database that give you a nicer dataschema to work with and then base your model on the views in combination with stored procedures.
Hope this gives you any ideas.
I'm building an ASP web application and for the moment I have a namespace called Queries that contains the linq queries that are called from the code behind pages. The whole site will initially contain about 40 queries; more will be added later.
Should I keep all my queries in one large namespace or should I create a namespace for the queries of each page? For instance, QueriesPageA, QueriesPageB, QueriesPageC... and end up with about 10 smaller namespaces.
Thanks.
It sounds like you're building a business logic layer.
If you're using LINQ to SQL or Entity Framework, you will already have a collection of entity classes that closely represent your business domain.
I prefer to add my queries to entity classes as static methods. This keeps my queries neatly distributed (so I don't end up with one huge business logic class) and easy to find (a query that retrieves a set of users will live in the User class).
If the query produces an aggregate or report (e.g. quantity of cookies sold grouped by year), then I usually create a new class for the report. That way, the report becomes a model in its own right, which works well in an MVC architecture, if that's something you're considering.
I think splitting them out is a good idea, but it would be more readable and less confusing to create namespaces based on the data they're returning instead of the page they're originally used on, e.g. MyApp.Queries.Customers, MyApp.Queries.Orders.
I am working on an iPhone app, and in a particular view I need to load two different entities: One that will populate a UITableView, and another that will populate a UITextView.
Is it possible to fetch both properties using a single NSFetchedResultsController?
Or do I need to use two different NSFetchedResultsControllers?
Any ideas on how to best approach this problem?
Each fetch request has only one entity and each fetched results controller has only one fetch. Therefore, you need separate controllers for each entity.
If you think about it, how would you make a predicate to fetch two logically separate entities?
You probably don't need two fetches at all. In most cases, you can fetch the entities that populate the table and then use a relationship for the entity of the selected row to populate something like a text view.
Best solution would be to refactor your Model and see if your 2 entities have something in common. You can make an abstract entity for the intersecting stuff, then inherit your 2 entities out of that. Perform the fetch on the abstract entity, and your fetch results controller should return mixed results.
As TechZen stated, the answer is no.
However, you can monitor the saves of the NSManagedObjectContext yourself and react to those saves. If you really do need to watch more than one entity (something that is far more common on the iPad than the iPhone) then add a NSNotification observer on the NSManagedObjectContextDidSaveNotification and look at the -userInfo of the NSNotification that comes back. You can then run predicates on against the results to determine if you need to update your display. That is what the NSFetchedResultsController is doing under the covers.
The quick answer is NO. But I found a creative answer.
In your tableViewController, make a search bar with how many scopes you have.
When different scope is selected, you can fetch different entities!
This works because I made an app like this!
Users would have easier time separating the two different data too!
Have been trying out the new Dynamic Data site create tool that shipped with .NET 3.5. The tool uses LINQ Datasources to get the data from the database using a .dmbl context file for a reference. I am interseted in customizing a data grid but I need to show data from more than one table. Does anyone know how to do this using the LINQ Datasource object?
If the tables are connected by a foreign key, you can easily reference both tables as they will be joined by linq automatically (you can see easily if you look in your dbml and there is an arrow connecting the tables) - if not, see if you can add one.
To do that, you can just use something like this:
<%# Bind("unit1.unit_name") %>
Where in the table, 'unit' has a foreign key that references another table and you pull that 'unit's property of 'unit_name'
I hope that makes sense.
(EDIT misunderstood the question, revising my answer to the following)
Your LinqDataSource could point to a view, which allows you to overcome the problem of not being able to express a Join in the actual element. From "How to: Create LINQ to SQL Classes Mapped to Tables and Views (O/R Designer)":
The O/R Designer is a simple object relational mapper because it supports only 1:1 mapping relationships. In other words, an entity class can have only a 1:1 mapping relationship with a database table or view. Complex mapping, such as mapping an entity class to multiple tables, is not supported. However, you can map an entity class to a view that joins multiple related tables.
You cannot put more than one object/datasource on a datagrid. You will have to build a single ConceptObject that combines the exposed properties of the part Entities. Try to use DB -> L2S Entities -> ConceptObject. You must be very contrived if the DB model matches the ConceptObject field-for-field.
You are best using a ObjectDataSource when you wnt to do more complex Linq and bind your Grid to the ObjectDataSource.
You do however need to watch out for Anonymous types that could give you some trouble, but anything is posible...