Navigation Properties between Views in EF Code First - entity-framework

Is it possible to define navigation properties between Views in EF with code first? Please, note that I do not need any tool generating any code for me. I would like to know whether or not this can get done and the logic behind this.
Suppose you have a project in which you will only work with views. No data will be created, updated or deleted (at least by you). All you need to do is queries and all you have are views.
Suppose now a second scenario in which you can also have tables, but your purpose is only to visualize data (you will treat them as views). However, there is not always a foreign key referencing the primary key of the parent table in relationships.
My question is: Can I define navigation properties in these scenarios? Or do I have to relay on joins only?
Thanks!

I'm working on this myself. Everything I'm reading indicates you have to manually edit the .edmx files so that the views are considered tables and manually add the nativation properties yourself. Downside is if you resync against the database you have to repeat the process all over again.
Re:
https://social.msdn.microsoft.com/Forums/en-US/dcbdfe52-6ba7-4e75-94aa-51c88b57635c/adding-navigation-property-to-a-view?forum=adodotnetentityframework

Related

Can we choose not to generate views by Entity?

We have a lots of Entity and we are trying to limit the number of SQL objects generated by CodeFluent Entity. We are using some lightViews but I have the feeling that a lot of generated views are useless.
Is it possible to decide Entity by entity to produce view or not ? (I'm aware of the produceViews option on the SQL producer but we still need some views)
No this is not an option. The produceViews attribute only related to persistent views explicitly defined in your model.
Now : if you are completely in control of what you can use, sure you are not referencing some default generated views in any model attribute, any methods (CFSQL or raw SQL), or partial code, then here some ideas :
You can choose not to use the default generated xx_views.sql :
- do not keep it in sourcecontrol
- provide your own views (the persistent known ones extraceted from the generated file) in a custom post SQL script after_[DefaultNamespace]_views.sql : in it you could for instance delete all existing views, then declare only the ones you want to use. It would be a manual step that can be time consuming if your persistent views are changing a lot between versions. May be a good name convention for persistent views will help this manual report for a good enough solution.
Another more advanced and cleaner idea would be to provide a custom aspect removing the "default" view created for each entity in the meta model before production (see infered meta model and CodeFluent.Model.Entity class), except for entities marked with a custom attribute. This is an idea not guaranted/tested.

Setting relationships in core data swift after context saved

Working with a core data model and I am still new this. Right now I have a view controller that saves the relationship between 2 entities and it stays fine until I have to add another relationship into the primary core data entity. When I do this the relationship between the initial 2 relationships breaks and the tableview is no longer able to present the relationship and thus crashes. I know in order to set the relationship I need to insert it into the already existing context but how do I actually go about that without clearing out the previously set relationhip?
Let's say I have a entity named primary and one named secondary. The relationship between these two sets fine. When I try to set the relationship for the third entity the relationship between the secondary and primary clears out. The real problem is that I am actually passing the object I want to store the relationship into so when I do code similar to this:
passedObject?.relationhips = entity.relation
it isn't actually setting the relationship. Am I missing something? Do I have to insert something into the primary entity context outside of setting the relationship?
A really common core data crash is when you've updated your model but the app on the simulator has data that uses the old model and crashes. So after updating your modal it's a good habit to delete your app from the simulator before running again (although I think Apple made a change in iOS9 where your app will migrate the data automatically but I've not checked that).
If that's not the problem it would help a lot to see a screen grab of your model to see how relationships between your entities are set up.
Because you're new to core data, I'd check to see if the relationship type if 'to one' (the default) or 'to many' (to use if you want a connection between a set of entities instead of just one).
Always check core data subclasses after you have them created automatically by Core Data. Sometimes the setup is incorrect as was the case with my problem.

DDD, EF and Referential Integrity

Ok so I have my roots defined. Entities inside a root are allowed references to other entities inside the same root, but not outside. There they can only have the identity or the related entity. This is all great.
But, I'm using EF5 and navigation properties are getting in the way. I really want to only define navigation properties where entities are inside the aggregate. But how can I apply some referential integrity where related entities are in different aggregates? Is the only option to add FKs manually using migrations?
And again, but... this is going to cause a bit of a problem because I want to be able to have ReadOnlyRepositories for fetching aggregated data from all over the place. I don't want to get into CQRS as there is too much of a leap there I feel. Am I stuck needing a second bounded context with a new set (or maybe derived set) of entities with extra navigation properties defined? All so i can write queries that pull data from more than one root?
Needing referential integrity usually indicates a deeper issue. Why do you need the entity identifier to exist in both tables? What is being held consistent that way? And why isn't that modeled explicitly? These questions are not as important as the answer to them. Also realize that by just using other technology over the same db schema (and proper indexes) many of your problems could go away. Who knows, you just might be doing CQRS at that point in time.

EF Code First and Database Views

I just started looking at Database Views with Code First... and try to decide if I should use them.
Here Ladislav recommends to use NotMapped inheritance parent for table and Db-View (my view only adds sums of child entities)... but how this work with CF Migrations? I really want to use them.
Also... navigation properties will work on Db-View Entity?
Is there any way to save data directly into Db-View entity (and it's table)?
If you want to use code first and migrations you should not use views. Views are database "logic" constructs and code first is not an approach for creating database logic. With code first you should use the projection which is also mentioned in the linked answer.
Migrations will not be able to detect changes related to your views. You will have to write all migration code for views manually.
If you want to use views you should do database first (= no migrations) and either map them with EDMX or code mapping.
Also... navigation properties will work on Db-View Entity?
This is the only scenario where code mapping provides better support than EDMX. You can define relation in your model even if it doesn't exist in the database (but your database must ensure data integrity). It is in theory possible with EDMX as well but it requires changing EDMX manually.
Is there any way to save data directly into Db-View entity (and it's table)?
Yes but your view must be updatable. I don't think that view with aggregation values is updatable.

EDMX Circular Navigation

We have determined that if you have two entities that are related and then you leave the default navigation properties between them (one pointing to the other and back), then saving Entity data is not possible. Only one navigation property can be kept.
For example:
ENTITY:USER
Property.UserId
NavigationProperty.Favorites
_
ENTITY:FAVORITE
Property.FavoriteId
Property.URL
Property.UserFk
NavigationProperty.Users
Does anyone know of an easy way to detect these circular navigation properties? I know one way is to look for them by hand - we're doing that, but we have hundreds of entities in our EDMX and "manual" approach is proving to not be a realiable option.
Thanks.
We have determined that if you have
two entities that are related and then
you leave the default navigation
properties between them (one pointing
to the other and back), then saving
Entity data is not possible. Only one
navigation property can be kept.
You have determined it wrong - saving entities works without any problem. The only scenario where this cause problems is serialization where it can be solved for some serializers with additional metadata.