Entity Framework 5 Complex Types - entity-framework

even if I done googlin' about that for more than hour, anyone knows if
in EF5 a Complex Types can participate in an association?
I know till EF4.x that wasn't possible, but now? there's an official documentation statement about that?
My need is mapping a class Person with Address Complex Types which refer a City
public class Person
{
public int ID {get;set;}
public string Name {get;set;}
public Address Address {get; set;}
}
public class Address
{
public string Street {get; set;}
public City City {get; set;}
}
public class City
{
public int ID {get;set;}
public string Name {get;set;}
}
I need to do a mapping with EntityTypeConfiguration<T> specifying the Complex Type association. It's that possible in EF5?
Thanks in advance
EDIT:
I saw that link complex type even if it refer to Entity Data Model, it says that it's not possible to map associations to Complex Types. It's that still true?

Even as of Entity Framework 6, complex types cannot contain navigation properties.

Related

EF Core, Primary Key is not auto generated for Entity which inherit from ICollection

Here is my Entity:
public class StackImage: ICollection<StackFile>
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual Guid Id { get; set; }
private IList<StackFile> StackFiles { get; set; } = new List<StackFile>();
public StackImage()
{
}
[...] // Implementation of ICollection
}
public class StackFile
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual Guid Id { get; set; }
public string Url { get; set; }
public int Position { get; set; }
public StackFile(){}
}
stackImage.Add(new StackFile(url));
stackImage= await _stackImageRepository.UpdateAsync(stackImage);
await _unitOfWork.SaveChangesAsync();
In this sample after UpdateAsync, the StackImage Id is not generated (stackImage.Id == default) but the StackFile Id is correctly generated (stackImage[0].Id == default)
Did you already noticed this problem? My guess is, EF Core see StackImage as a list and doesn't try to generate a new Guid. How to fix this issue?
EDIT:
From what I can read on the web and by responses I received, It seems not possible to do it. If someone has the solution, please let us know :)
It seems to me that you want to design a database with (at least) two tables. A table with StackImages and a table with StackFiles.
You want to design a one-to-many relation between StackImages and StackFiles: every StackImage has zero or more StackFiles, every StackFile belongs to exactly one StackImage. In a database this is implemented using a foreign key.
Hence, it is not true that a StackImage is a StackFile. However, you can say that a StackImage has some StackFiles.
Following the entity framework code first conventions your classes should be similar to:
class StackImage
{
public Guid Id {get; set;}
...
// every StackImage has zero or more StackFiles (one-to-many):
public virtual ICollection<StackFile> StackFiles {get; set;}
}
class StackFile
{
public Guid Id {get; set;}
...
// every StackFile belongs to exactly one StackImage, using foreign key:
public Guid StackImageId {get; set;}
public virtual StackImage StackImage {get; set;}
}
finally the DbContext:
class MyDbcontext : DbContext
{
public DbSet<StackImage> StackImages {get; set;}
public DbSet<StackFile> StackFiles {get; set;}
}
Note the use of virtual properties to express the relations between the tables. As the foreign key StackImageId is supposed to be a real column, it is not virtual
In entity framework the columns of a table are represented by non-virtual properties,
the virtual properties represent the relations between the tables.
Because I followed the conventions, there is no need for attributes, nor fluent API. Entity framework detects the one-to-many collection and creates the proper tables for you. Only if you want different identifiers for your tables or columns you'll need fluent API or attributes.

Entity Framework Code First - Setting Foreign Key / Navigation Property

I like to reference to a specific entry in a database. Is there a data annotation, which I could use
E.g.
public class Address
{
public int CityId {get; set;}
}
public class City
{
public int id {get; set;}
}
So the Address.CityId references the City.id.
And how can I reference to Table Columns via data annotations.
Please refer this link .
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
in that read Topic : The Key Attribute
What you want is called One-To-One entity mapping. You can create navigation property in your Address class as follows:
public class Address
{
[Key]
public int AddressID {get;set;}
public int CityID {get; set;}
[ForeignKey("CityID")]
public City City {get;set;}
}
public class City
{
[Key]
public int CityID {get; set;}
}
Add ForeignKey attribute to your navigation property to create mapping through foreign key. And do not forget to define key attributes in each EF model.
More details on Entity Framework home page: Configure One-to-Zero-or-One Relationship

Entity Framework, Unneeded Foreign key

I'm using EF Code first.
I created two classes. For simplicity, imagine that I have a User table (class) and a FileAttachment table. I want to use the FileAttachment table with many other classes, so that any part of the application that requires having a FileAttachment can reuse that table. The problem is that when EF generates the schema, it creates a Foreign Key in the FileAttachment table back to User table. Is there a way to disable that?
Thanks
You need to build an intermediate class.
public class UserDocument
{
public int Id {get;set;}
public int UserId {get;set;}
public virtual User User {get;set;}
public int FileAttachmentId {get;set;}
public virtual FileAttachment FileAttachment {get;set;}
}
So your user class can now have:
public virtual ICollection<UserDocument> Documents {get;set;}
And in this case, FileAttachment class will not have reference to User.
If you want now to build some other document type, just implement another intermediate type, i.e. imagine you want to have CustomerDocument:
public class CustomerDocument
{
public int Id {get;set;}
public int CustomerId {get;set;}
public virtual Customer Customer {get;set;}
public int FileAttachmentId {get;set;}
public virtual FileAttachment FileAttachment {get;set;}
}
And then your hypothetical Customer class would have:
public virtual ICollection<CustomerDocument> Documents {get;set;}

Automatically update a RelatedProperty when adding a new entity to the collection in a one-to-many relationship

EntityFramework 5.0
Suppose I have the following setup:
public class Book
{
public int ID {get; set;}
public string Title {get; set;}
[InverseProperty("Books")]
[Required]
public Author Author {get; set;}
}
public class Author
{
public int ID {get; set;}
public string Name {get; set;}
public virtual ICollection<Book> Books {get; set;}
}
Then in my code I create a new Book and I do this:
author.Books.Add(newBook);
How can I have the Book pick-up its Author automatically instead of having to write this every time:
newBook.Author = author;
I want the child entity to pick up its parent automatically when added to the parent's collection.
Is this possible? Fluent mapping maybe?
Or do I have to maintain both sides of this bi-directional relationship myself?
My mistake.
This is the default behavior and the book gets its Author out-of-the-box.
Case closed.

Update ONLY some fields of attached Entity

I'm using .NET 3.5 SP1. I have entity 'AppUser':
public class AppUser : System.Data.Objects.DataClasses.EntityObject{
public int Uid {get; set;}
public string UserName {get; set;}
public string Password {get; set;}
public DateTime LastLogin {get; set;}
public string Name {get; set;}
public string Address {get; set;}
public string Comment {get; set;}
...........
}
To update ALL fields of an attached enitity:
public void Update(AppUser updateUser) {
AppUser user = ctx.AppUserSet.Where(u => u.UserId == userId).FirstOrDefault();
//This will update ALL fields
ctx.ApplyPropertyChanges(user.EntityKey.EntitySetName, updateUser);
ctx.SaveChanges();
}
I want to update all fields except Password and LastLogin. I can update individual fields, but will be cumbersome for entities with large number of fields.
Please tell, what is the best way to achive this?
Thank You.
Change the setters for those two properties to private in the EDMX/model.
I don't think that you can do this directly with EF without extra programming effort.
There are three possibilities:
Update through a view
Update using a stored procedure
Change your data model, such that the password and lastlogin are in a seperate table with a one to one relationship.
All of these require extra programming effort.