简体   繁体   中英

How does Entity Framework handle interfaces?

I created a .net standard library with the model of my database, so I can use it in asp.net core app and Xamarin because I want to get/post data from mine web api (in asp) to the phone app (xamarin)

So I've successfully made it work! But the problem is that Xamarin doesn't support identity, so to solve this I've introduced an interface IUser of my own, and used it in my templates.

Here it is an example of the user on the asp.net part

namespace WebApp {
   public class ApplicationUser : IdentityUser, IUser { /*some code*/ }

   public class ApplicationDbContext : IdentityDbContext< ApplicationUser > {
      public DbSet< Event<ApplicationUser>> Events { get; set; }
   }
}

I used generics to make all the stuff to work so in the library:

namespace Models {
  public interface IUser{
      string Id { get; set; }
      string UserName { get; set; }
      string Email { get; set; }
  }

public class Event <TUser> where TUser : IUser{
    public virtual int Id { get; set; }

    [Required, MinLength( DomainConstraints.EventNameMinLen )]
    public virtual string Name { get; set; }

    [Required]
    public virtual string Description { get; set; }

    [Required]
    public virtual string ApplicationUserId { get; set; }

    public virtual TUser ApplicationUser { get; set; } //creator of the event
}
}

Every time I need to write Event<ApplicationUser> in asp.net and Event<User> in Xamarin, is there a better way to use this? maybe only with interface, but then how EF handles it?

If I understand correctly, the inconvenience comes from the part where you have to inherit from ASP's IdentityUser: class ApplicationUser : IdentityUser {}

Which version of ASP.NET MVC are you using? In a newer versions it seems possible not to inherit from IdentityUser. In case if you don't inherit from IdentityUser, you would move your ApplicationUser to your .net standard library.

MVC 5: Should I inherit my User from IdentityUser class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM