Asp.net EF how to create 1 to many table relation - entity-framework

I have a class in my model with authorized users list:
public class Album
{
public virtual List<Photo> Photos { get; set; }
public List<User> AuthorizedUsers { get; set; }
public long Id { get; set; }
[ForeignKey("CreatorId")]
public User Creator { get; set; }
public string CreatorId { get; set; }
public DateTime CreatedDate { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public long CoverPhotoId { get; set; }
}
Now I would like EF to create a table such:
AlbumAuthorized where at each album Id many users Id will correspond.
So that when I pick an album I will know which users are authorized to see it, by loading the list of authorized users (using the mentioned table). Ho do I tell EF that?
I hope my question is clear.

Related

Issue with Include in Entity framework

I have Employee, Address and Organization classes. below are the details
public partial class Employees
{
public Employees()
{
AddressDetails = new HashSet<AddressDetails>();
OrganizationDetails = new HashSet<OrganizationDetails>();
}
public string Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public ICollection<AddressDetails> AddressDetails { get; set; }
public ICollection<OrganizationDetails> OrganizationDetails { get; set; }
}
public partial class AddressDetails
{
public int Id { get; set; }
public string Address { get; set; }
public string Type { get; set; }
public string EmployeeId { get; set; }
public Employees Employee { get; set; }
}
public partial class OrganizationDetails
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string EmployeeId { get; set; }
public Employees Employee { get; set; }
}
I have used fully defined relationship, you can see Employee has collections of both AddressDetails and OrganizationDetails as navigation properties. And each of them have EmployeeId and Employee in the same way.
My problem is, when i try to fetch Employee details using Include(), both AddressDetails and OrganizationDetails are loaded that's fine, but when i checked both the collections each entity has again loaded Employee information and so on.
for example: if i check AddressDetails collection which is loaded, Address object has information of Employee and again that Employee has collections of both AddressDetails and OrganizationDetails.
Please help me how can i avoid this. I don't want to remove Employee object property from AddressDetails and OrganizationDetails. is there anyway to make it work with Include().
here is the query i'm using to load these navigation properties.
List employees = _context.Employees.Include(emp => emp.AddressDetails).Include(emp => emp.OrganizationDetails).ToList();

Storing IEnumerable<string> in Entity Framework

Is it possible to store an IEnumerable<string> in Entity Framework?
I'm using code-first in ASP.NET MVC5 and I have a model that looks a little like this, but ImageUris does not appear as a column in my database (all the other properties do).
public class Product
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Condition { get; set; }
public decimal Price { get; set; }
public IEnumerable<string> ImageUris { get; set; }
}
PS: In case you are interested in why I'm storing Uris rather than images themselves, they are uris to Azure Storage Blobs.
You cannot save multiple records in single column of the relational database. There is no such data type that supports this.
You can create a separate table for Image Uris and then store your image Uris there.
Your entity code would look something like this:
public class Product
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Condition { get; set; }
public decimal Price { get; set; }
public virtual ICollection<ImageUri> ImageUris { get; set; }
}
public class ImageUri
{
[Key]
public int Id { get; set; }
public string Uri { get; set; }
}

Multi-Tenant and EF6 Design

I'm designing a multi tenant website using EF6 code first, MVC, and other from the MS stack.
I want to have announcements for each Tenant. Simple enough, my EF code first class would look something like this:
class Announcement
{
public Announcement()
{
DateCreated = DateTime.Now;
}
[Key]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public virtual Tenant Tenant { get; set; }
public ApplicationUser Author { get; set; }
}
My design question is what if I want the site administrator to have the ability to post announcements to all tenants?
Since the database will force the Tenant relationship, I can't set the Tenant property to something artificial.
Before EF, I'd do something like this, but now I will lose the nice-to-have EF Navigation property.
class Announcement
{
public Announcement()
{
DateCreated = DateTime.Now;
}
[Key]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public int TenantID { get; set; }
public ApplicationUser Author { get; set; }
}
I would use TenantID appropriately for all Tenant based announcements, but would set it to 0 for site wide announcements.
So, is there a better design (other than two classes/tables) that can still leverage the EF Navigation properties?
What is the issue with
class Announcement
{
public Announcement()
{
DateCreated = DateTime.Now;
}
[Key]
public virtual int Id { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual string Title { get; set; }
public virtual string Message { get; set; }
public virtual int TenantID { get; set; } // during insert/Update set as required. the tenant should exist.
// A dummy SYSTEM wide tenant may be an option to consider
// navigation props
[ForeignKey("TenantID")]
public virtual Tenant Tenant { get; set; } // <<<<< NAV as before
public virtual ApplicationUser Author { get; set; }
}

Mapping Many to many in Entity framework

simply I ask this How to Map , How to ProductCustomer in the sample ??
public class ProductCustomer
{
public virtual Product Product { get; set; }
public virtual Customer Customer { get; set; }
}
and about Product and Customer :
public class Customer
{
public int Id { get; set; }
public string CustomerName { get; set; }
}
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public decimal Amount { get; set; }
}
thanks!
You don't need to create the ProductCustomer object.
In EF, you create your Customer and Product, and then you create collections to each. This will automatically create the proper link tables.
public class Customer
{
public int Id { get; set; }
public string CustomerName { get; set; }
public virtual List<Product> Products {get;set;}
}
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public decimal Amount { get; set; }
public virtual List<Customer> Customers {get;set;}
}
This is only the case, however, if your link table has no payload (has no additional data). If it does, then you will need to create the link table as an entity similar to what you originally did, but you add 1:many links in your product and customer classes to the link entity. You then have to modify your queries to query through the link table.

ADO.NET EF what is the purpose of using List<> in the entities

Right now I'm learning ADO.NET Entity Framework and there's one thing that I can't explain to myself. Here is a source code from a tutorial I've been using recently:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public User UserId { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string DisplayName { get; set; }
}
First I thought that the using of List<> is the way to implement Foreign Key-like behaviour but now knowing that's not the case why we need and for what purpose we use List<> in our entites?
To show that Blog have a lot of Posts, when you will build your project in DB will be the relation 1xBlog--->NxPost where N=unlimited. This will show that each Blog can have unlimited amount of Posts