草庐IT

c# - 值不能为空。参数名称 : extent

coder 2024-05-28 原文

我首先使用 EF6 代码来创建我的数据库。昨晚一切正常,现在当我运行 update-database 命令时,出现以下异常:

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.ArgumentNullException: Value cannot be null.
Parameter name: extent
   at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
   at System.Data.Entity.Core.Mapping.StorageEntitySetMapping..ctor(EntitySet extent, StorageEntityContainerMapping entityContainerMapping)
   at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet)
   at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Value cannot be null.
Parameter name: extent

这是上下文类

public class WheelTrackDb : DbContext
{
    public WheelTrackDb(): base("DefaultConnection"){ }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Driver>()
            .HasRequired(d => d.Vehicle)
            .WithOptional(v => v.Driver)
            .Map(m => m.MapKey("VehicleId"));

        modelBuilder.Entity<TransactionLog>()
           .HasRequired(t => t.Acceptor)
           .WithMany()
           .HasForeignKey(t => t.AcceptorId)
           .WillCascadeOnDelete(false);

        modelBuilder.Entity<TransactionLog>()
           .HasRequired(t => t.Donor)
           .WithMany()
           .HasForeignKey(t => t.DonorId)
           .WillCascadeOnDelete(false);

        modelBuilder.Entity<Violation>()
            .HasRequired(v => v.GSMDeviceLog)
            .WithMany(g => g.Violations)
            .HasForeignKey(v => v.GSMDeviceLogId);

        modelBuilder.Entity<RouteFence>().Ignore(r => r.PolygonVertices);

        modelBuilder.Entity<Vehicle>()
            .HasRequired(v => v.License)
            .WithRequiredPrincipal();

        modelBuilder.Entity<MessageCounter>()
            .HasRequired(m => m.License)
            .WithOptional(l => l.MessageCounter)
            .Map(m => m.MapKey("LicenseId"));

        modelBuilder.Entity<GSMDeviceLog>()
            .HasRequired(g => g.Vehicle)
            .WithMany(t => t.GsmDeviceLogs);

    }

    public DbSet<UserInfo> UserInfos{ get; set; }
    public DbSet<TransactionLog> TransactionLogs{ get; set; }
    public DbSet<Driver> Drivers { get; set; }
    public DbSet<Vehicle> Vehicles{ get; set; }
    public DbSet<VehicleGroup> VehicleGroups { get; set; }
    public DbSet<License> Licenses { get; set; }
    public DbSet<Policy> Policys { get; set; }
    public DbSet<GSMDeviceLog> GSMDeviceLogs { get; set; }
    public DbSet<MessageCounter> MessageCounters { get; set; }
    public DbSet<Violation> Violations { get; set; }
    public DbSet<OwnershipPaper> OwnershipPapers { get; set; }
    public DbSet<Subscriber> Subscribers { get; set; }
}

这是配置:

internal sealed class Configuration : DbMigrationsConfiguration<WheelTrack.Models.WheelTrackDb>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }


    protected override void Seed(WheelTrack.Models.WheelTrackDb context)
    {

    }
}

我搜索了互联网,发现我应该只在我的上下文类中有属性。我检查了三次,它只有 dbset 属性。我无法弄清楚发生了什么。任何帮助将不胜感激。谢谢。

最佳答案

发现问题。实际上,我有一个 HttpPostedFileBase EF 无法绑定(bind)到数据库列的 UserInfo 模型中的属性。

在其上放置一个未映射的属性使其工作。

    [NotMapped]
    public HttpPostedFileBase Photo { get; set; }

因此,对于遇到此问题的其他人,请确保两件事:

  1. 您的上下文应始终包含 DbSets 属性。例如-

    public DbSet<Policy> Policies { get; set; }

  2. 模型类中不应有 EF 无法绑定(bind)到数据库列的类型。

希望对您有所帮助。

关于c# - 值不能为空。参数名称 : extent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21277078/

有关c# - 值不能为空。参数名称 : extent的更多相关文章

  1. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  2. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  3. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  4. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  5. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  6. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  7. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  8. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  9. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  10. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

随机推荐