简体   繁体   English

'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'未标记为可序列化

[英]'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' is not marked as serializable

I am implementing ASP.NET Identity in a system where used authentication with a User Model with Entity Framework and being controlled in WebApp by Session (yes it is a legacy system into web forms), we implemented, replaced the User model by ApplicationUser of Identity, everything working OK. 我正在一个系统中实现ASP.NET Identity,在该系统中,使用了带有Entity Framework的用户模型进行身份验证,并由Session在WebApp中进行了控制(是的,这是Web表单中的遗留系统),我们实现了,将User模型替换为Identity的ApplicationUser ,一切正常。

The problem is, there is a notification system, which is basically a Many to Many relationship between a Notification and User Model, these notifications are saved in SQL Server along with the rest of the system, but are also in a Cache Redis for faster reading and needs to be serialized for writing on it. 问题是,有一个通知系统,它基本上是NotificationUser模型之间的多对多关系,这些通知与系统的其余部分一起保存在SQL Server中,但也保存在缓存Redis中,以加快读取速度并且需要对其进行序列化以进行编写。 We remove the User model, then added an ICollection of ApplicationUser in the Notification model and vice versa - making the relate between both. 我们删除了User模型,然后在Notification模型中添加了ApplicationUserICollection ,反之亦然-两者之间建立了联系。

But ApplicationUser inherits from IdentityUser , and even adding the annotation [Serializable] I get the exception: 但是ApplicationUser继承自IdentityUser ,甚至添加了注释[Serializable]我得到了一个例外:

Type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' the assembly 'Microsoft.AspNet.Identity.EntityFramework, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' is not marked as serializable 类型'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'程序集'Microsoft.AspNet.Identity.EntityFramework,版本= 2.0.0.0,区域性=中性,PublicKeyToken = 31bf3856ad364e35'未标记为可序列化

My question is, is there any way to serialize this? 我的问题是,有什么方法可以序列化吗? Or I will have to create another user model only to relate with notifications model? 还是我将不得不创建另一个仅与通知模型相关的用户模型?

ApplicationUser Model ApplicationUser模型

[Serializable]
public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        this.Notificacoes = new List<Notificacao>();
    }

    public ClaimsIdentity GenerateUserIdentity(IdentityConfig.ApplicationUserManager manager)
    {
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(IdentityConfig.ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
    public bool ReceiveNotifications { get; set; }
    public int Permission { get; set; }
    public virtual ICollection<Notificacao> Notificacoes { get; set; }
}

Notification Model Notification模型

[Serializable]
public partial class Notificacao
{
    public Notificacao()
    {
        this.Usuarios = new List<ApplicationUser>();
    }

    public int Codigo { get; set; }
    public string Mensagem { get; set; }
    public DateTime DataHoraNotificacao { get; set; }
    public int Tipo { get; set; }
    public virtual ICollection<ApplicationUser> Usuarios { get; set; }

}

Serialize Method used to Serialize a object to Redis (where it throws the exception) 用于将对象序列化为Redis的Serialize方法(引发异常的地方)

static byte[] Serialize(object o)
{
    if (o == null)
    {
        return null;
    }

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream())
    {
        binaryFormatter.Serialize(memoryStream, o);
        byte[] objectDataAsStream = memoryStream.ToArray();
        return objectDataAsStream;
    }
}

As you have no control over the assemly containing the IdentityUser class this cannot be done. 由于您无法控制包含IdentityUser类的组件,因此无法执行此操作。 An alternative solution could be to map your object to Dto's (data transfer objects) before caching, and the other way around upon fetching. 另一种解决方案是在缓存之前将对象映射到Dto(数据传输对象),而在获取时将其映射。

Other topics on the subject (related to WCF but still involves serializing IdentityUser): 关于此主题的其他主题(与WCF相关,但仍涉及序列化IdentityUser):

asp-net-mvc-5-identityuser-in-wcf ASP净MVC-5-identityuser功能于WCF

how-do-i-serialize-an-identityuser-reference-in-web-api-2-2 怎么办,我序列化-AN-identityuser参考功能于网络的API-2-2

You don't want to serialize IdentityUser or any of it's sub-classes to the database. 您不想将IdentityUser或其任何子类序列化到数据库。 Period. 期。

If you need to maintain relationships in database, create a POCO (Plain Old CLR Object - In simple words, a custom class). 如果需要维护数据库中的关系,请创建POCO (普通的旧CLR对象-简单来说就是自定义类)。

Also, if you need to carry around IdentityUser in your user class, use composition instead of inheritance . 另外,如果您需要在用户类中携带IdentityUser ,请使用composition而不是继承 Which means, don't inherit from IdentityUser but create a property of it in your class. 这意味着,不要从IdentityUser继承,而是在您的类中创建它的属性。 And mark that property as NonSerializable . 并将该属性标记为NonSerializable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的外键? - Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser? 没有从 ApplicationUser 到 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的隐式引用转换 - There is no implicit reference conversion from ApplicationUser to Microsoft.AspNet.Identity.EntityFramework.IdentityUser 为什么IdentityUser类在Microsoft.AspNet.Identity.EntityFramework命名空间中而不在Core包中? - Why is the IdentityUser class in the Microsoft.AspNet.Identity.EntityFramework namespace and not in the Core package? .NET Standard 2.0中的Microsoft.AspNet.Identity和Microsoft.AspNet.Identity.EntityFramework - Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in .NET Standard 2.0 自定义Microsoft.AspNet.Identity.EntityFramework.UserStore <TUser> - custom Microsoft.AspNet.Identity.EntityFramework.UserStore<TUser> 无法从“字符串”转换为“ Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole - cannot convert from 'string' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole vsts构建在Microsoft.AspNet.Identity.EntityFramework上失败 - vsts build fails on Microsoft.AspNet.Identity.EntityFramework Nuget错误:“ EntityFramework 5.0.0”与“ Microsoft.AspNet.Identity.EntityFramework 1.0.0不兼容” - Nuget error: 'EntityFramework 5.0.0' is not compatible with 'Microsoft.AspNet.Identity.EntityFramework 1.0.0 来自 api 的实体框架身份注册,具有自定义身份用户 - Entityframework identity register from api with custom identityUser Microsoft.AspNet.Identity.EntityFramework.dll中发生类型为“ System.ArgumentNullException”的异常 - An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM