简体   繁体   English

EntityFramework.Core.dll中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理

[英]An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code

I am trying to get the long value associated to an ActivityLogType it is associated to either a create , edit , and delete record in the database. 我试图获取与ActivityLogType相关联的long值,它与数据库中的createeditdelete记录相关联。

It is used for an audit / activity log that is needed. 它用于所需的审计/活动日志。

public enum ActivityLogType
{
    Create = 1,
    Update = 2,
    Delete = 3 
}

My getter method: 我的getter方法:

public ActivityType GetType(ActivityLogType type)
{
    var id = (long)type;

    Console.WriteLine(id); // <---- this produces a 1 in the console. So the cast works?

    return _context.ActivityTypes.Where(x => x.Id == id).FirstOrDefault(); // <-- This line throws the error 
}

UPDATE 1 更新1

un-lucky suggested using (long)ActivityLogType.Create to get the desired output. un-lucky建议使用(long)ActivityLogType.Create来获得所需的输出。 I tried this: (Still not working) 我试过这个:(仍然没有工作)

public ActivityType GetType(ActivityLogType type)
{

    switch (type)
    {
        case ActivityLogType.Create:
            return _context.ActivityTypes.Where(x => x.Id == (long)ActivityLogType.Create).FirstOrDefault();
        case ActivityLogType.Update:
            return _context.ActivityTypes.Where(x => x.Id == (long)ActivityLogType.Update).FirstOrDefault();
        case ActivityLogType.Delete:
            return _context.ActivityTypes.Where(x => x.Id == (long)ActivityLogType.Delete).FirstOrDefault();
        default:
        return null;
    }

}

UPDATE 2 更新2

Here is the ActivityType entity 这是ActivityType实体

public class ActivityType
{

    public ActivityType()
    {
        this.Activities = new HashSet<Activity>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    [Display(Name = "Label")]
    public string Label { get; set; }

    [Display(Name = "Display Order")]
    public int Order { get; set; }

    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }

    [Display(Name = "Activities")]
    public virtual ICollection<Activity> Activities { get; set; }
}

你可以试试这个:

long id= Int64.Parse(Convert.ChangeType(type, Enum.GetUnderlyingType(type.GetType())).ToString());

暂无
暂无

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

相关问题 .DLL中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidCastException' occurred in .DLL but was not handled in user code 我不断收到mscorlib.dll中发生的&#39;System.InvalidCastException&#39;类型的异常,但未在RetailPrice的用户代码中处理 - I keep getting An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code for RetailPrice EntityFramework.dll中发生类型为&#39;System.Data.Entity.Core.MappingException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.Entity.Core.MappingException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.Data.SqlClient.SqlException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.SqlServer.dll中发生类型为&#39;System.Reflection.TargetInvocationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码C#中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, C# EntityFramework.dll中发生类型&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理:执行存储过程 - An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但在注册时未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code while Registering EntityFramework.SqlServer.dll中发生类型为&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM