简体   繁体   English

LINQ实体框架值不能为null。 参数名称:字符串

[英]LINQ Entity Framework Value cannot be null. Parameter name: String

I'm trying to return a collection of view-model objects to my view, from my Entity data-model and get the following error: 我试图从我的实体数据模型返回视图模型对象的集合到我的视图,并得到以下错误:

Value cannot be null. 值不能为空。 Parameter name: String Description: An unhandled exception occurred during the execution of the current web request. 参数名称:字符串 说明:在执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: String

    Source Error:


Line 22:         {
Line 23:             IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.Where(cc => cc.CollegeContactID >0).AsEnumerable().ToList()
Line 24:                 .Select(c => new CollegeContactViewModel()
Line 25:                 {
Line 26:                     Id          = c.CollegeContactID,

Here is my index action 这是我的索引动作

public ActionResult Index()
        {
            IEnumerable<CollegeContactViewModel> contacts = db.CollegeContacts.AsEnumerable().ToList()
                .Select(c => new CollegeContactViewModel()
                {
                    Id          = c.CollegeContactID,
                    Status      = c.CollegeContStatus,
                    Center      = c.CollegeContCenter,
                    Salutation  = c.CollegeContSalutation,
                    FirstName   = c.CollegeContFname,
                    LastName    = c.CollegeContLname,
                    Position    = c.CollegeContPosition,
                    Department  = c.CollegeContDept,
                    Institution = c.CollegeContInstitution,
                    Address = new Address()
                    {
                        AddressLine1 = c.CollegeContAdd1,
                        AddressLine2 = c.CollegeContAdd2,
                        City         = c.CollegeContCity,
                        State        = c.CollegeContSt,
                        PostalCode   = c.CollegeContZip
                    },
                    Email                = c.CollegeContEmail,
                    Phone                = c.CollegeContPhone,
                    Opt                  = c.CollegeContOpt,
                    ContactDate          = c.CollegeContDate,
                    OnMailingList        = c.CollegeContMailListSingle,
                    NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),
                    DateSent             = c.Datesent.ToString(),
                    DateEntered          = c.DateEntered.ToString(),
                    Reception            = c.Reception,
                    Partner              = c.Partner,
                    Website              = c.SAwebsite,
                    Poster               = c.CollegeContPoster
                });
            return View(contacts.ToList());
        }

The CollegeContactViewModel CollegeContactViewModel

public class CollegeContactViewModel
    {
        public int Id { get; set; }
        public string Status { get; set; }
        public string Center { get; set; }
        public string Salutation { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public string Department { get; set; }
        public string Institution { get; set; }
        public Address Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Opt { get; set; }
        public string ContactDate { get; set; }
        public bool? OnMailingList { get; set; }
        public int NumberOfCatalogsSent { get; set; }
        public string DateSent { get; set; }
        public string DateEntered { get; set; }
        public string Reception { get; set; }
        public string Partner { get; set; }
        public string Website { get; set; }
        public string Poster { get; set; }
    }

I have the exact same type of code for another entity in my database and it works just fine. 对于数据库中的另一个实体,我具有完全相同的代码类型,并且工作正常。 Also all the fields, with the exception of the primary key, allow null, so that can't be it. 此外,除主键外,所有字段都允许为null,因此不能为空。 I've also stepped through the code and the Id is getting populated. 我也逐步浏览了代码,然后填充了ID。

Can somebody please tell me why I keep getting this exception? 有人可以告诉我为什么我不断收到此异常吗?

Scanning your code for a string parameter, this looks like the most likely (only?) candidate: 扫描您的代码中的字符串参数,这看起来像是最有可能(仅?)的候选者:

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent),

If c.NumCatalogsSent is null, that is the error you'd get. 如果c.NumCatalogsSent为null,则将得到错误。


I'd try this instead (EF v4 can translate the null-coalescing operator): 我会改用这种方法(EF v4 可以转换null-coalescing运算符):

NumberOfCatalogsSent = int.Parse(c.NumCatalogsSent ?? "0"),

暂无
暂无

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

相关问题 值不能为null。参数名称:String - Value cannot be null. Parameter name: String “值不能为空。 在实体框架上运行嵌套查询时,“参数名称:源” - “Value cannot be null. Parameter name: source” when running a nested query on entity framework 实体框架代码的第一个例外:值不能为null。 参数名称:来源 - Entity Framework Code First Exception: Value cannot be null. Parameter name: source 使用Entity Framework在POST上获取错误-值不能为null。 参数名称:来源 - Getting error on POST with Entity Framework - Value cannot be null. Parameter name: source 如何修复 Entity Framework Core 错误“值不能为空。参数名称:frameworkName”? - How to fix Entity Framework Core error "Value cannot be null. Parameter name: frameworkName"? 如何解决 Value 不能为 null。 参数名称:linq 中的源? - How to resolve Value cannot be null. Parameter name: source in linq? ArgumentNullException:值不能为null。 参数名称:实体 - ArgumentNullException: Value cannot be null. Parameter name: entity System.ArgumentNullException:“值不能是 null。 参数名称:实体” - System.ArgumentNullException: “Value cannot be null. Parameter name: entity” 带有SQL Server CE数据库的实体框架:多对多关系给出了例外,“值不能为null。 参数名称:来源” - Entity Framework with SQL Server CE database: many-to-many relation gives exception “Value cannot be null. Parameter name: source” 值不能为空。 参数名称:路径 - Value cannot be null. Parameter name: path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM