Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow owned entities on a keyless entity type #17890

Closed
Tracked by #827
aherrick opened this issue Sep 17, 2019 · 8 comments
Closed
Tracked by #827

Allow owned entities on a keyless entity type #17890

aherrick opened this issue Sep 17, 2019 · 8 comments

Comments

@aherrick
Copy link

aherrick commented Sep 17, 2019

I have a simple view in my DB that is exposing some fields joined from multiple tables.

I'm able to map my view to my object similar to below:

	public IQueryable<MyDbView> MyDbView => Query<MyDbView>();

        modelBuilder.Query<MyDbView>().ToView(nameof(MyDbView));

MyDbView is an object similar to below.

		public class MyDbView
		{
			public string Name { get; set; }

			public Address OriginAddress { get; set; }
			public Address DestinationAddress { get; set; }
		}

		public class Address
		{ 
                        public int Id { get; set; }
			public string Name { get; set; }

			public string City { get; set; }
			public string State { get; set; }
		}

Since the view has flattened the 2 Address fields into 6 fields (OriginAddressName, OriginAddressCity, etc.), I'd like to inflate them back to the object so MyDbView doesn't have to have a direct mapping to each of the fields and I can reuse my types.

How is the possible?

@smitpatel
Copy link
Contributor

@aherrick - Can you elaborate more exactly what are you trying to achieve? What is the view definition and what part is causing issue?

@aherrick
Copy link
Author

aherrick commented Sep 17, 2019

Let's pretend my view looked like the following:

		Select  s.Name					'Name',
				oa.Id                       'OriginAddressId',
				oa.Name                     'OriginAddressName',
				oa.City						'OriginAddressCity',
				oa.State                    'OriginAddressState',
				da.Id                       'DestinationAddressId',
				da.Name                     'DestinationAddressName',
				da.City						'DestinationAddressCity'
				da.State                    'DestinationAddressState',
				
		From   Shipment s
		Join Address oa
			On s.Originaddressid = oa.Id
		Join Address da
			On s.Destinationaddressid = da.Id

I'd like to inflate this View into the MyDbView object.

@smitpatel
Copy link
Contributor

@aherrick - That is not possible. In EF each entityType maps to one database object. Same database object can be used for multiple entityTypes but you cannot specify a database object to materialize a graph of object.
The closest we have in backlog is #14525 which is about using result of FromSql to materialize whole graph. You can perhaps use it to do basic select from view.
Another alternative we have tracking is #11296 which allows you to specify raw SQL query for include.

If above was an aggregate root, there could have been more options but keyless entity types cannot act as one.

@aherrick
Copy link
Author

Hmm Thanks... I was hoping I could do something similar to below so I could just manually map the fields.

modelBuilder.Entity<Entity>().OwnsOne(
    o => o.OriginAddress,
    sa =>
    {
        sa.Property(p => p.Street).HasColumnName("ShipsToStreet");
        sa.Property(p => p.City).HasColumnName("ShipsToCity");
     // etc.
    });

@smitpatel
Copy link
Contributor

@aherrick - I also thought that is probably what you intend. But keyless entities cannot be principal end of relationship hence they cannot have owned entities.

@smitpatel smitpatel added this to the Backlog milestone Sep 20, 2019
@smitpatel smitpatel changed the title EF Core SQL View Map Fields to Complex Property on Object Map aggregate to database object (View/Table) Sep 20, 2019
@ilmax ilmax mentioned this issue Nov 4, 2019
16 tasks
@AndriySvyryd AndriySvyryd changed the title Map aggregate to database object (View/Table) Allow owned entities on a keyless entity type Feb 21, 2020
@kgaska
Copy link

kgaska commented Mar 12, 2021

Is there any reason why keyless entity cannot have owned type which origin is from the exactly same table? It would be very useful to be able simplify/reuse entity model for views or tables without primary key.

@kislovs
Copy link

kislovs commented Aug 24, 2021

+1 for ability to use OwnsOne on keyless entity, because it fit best when you need use only mappings from EF Core for Set<T>.FromSql[Raw|Interpolated](...).

@igorgiovannini
Copy link

+1

@ajcvickers ajcvickers removed this from the Backlog milestone Nov 10, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants