具有子对象集合的 MVC3 对象

发布于 2024-12-08 20:50:06 字数 710 浏览 6 评论 0原文

我有一个 C#.Net 中的 MVC3 项目。我有 Object1 和 Object2 列表...

    //navigation Property(s)
    public virtual ICollection<Object2> Object2s { get; set; }

Object2 有一个 Object1 属性和一个 Object1_Id 属性

    public int Object1_Id { get; set; }
    public virtual Object1 Object1 { get; set; }

当我运行 Object2 的索引视图时,我收到消息“无效的列名称 'Object1_Id1'”。奇怪......它正在寻找一个自动化的专栏。当我从 Object2 中删除 Object1_Id 时,它会正常工作,直到我转到“创建视图”中的“保存”。然后它会抛出一条错误消息:

{"Cannot insert the value NULL into column 'Object1_Id', table 'dbo.Object2'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

我确信我只是错过了一个简单的步骤。有什么想法吗?

I have an MVC3 Project in C#.Net. I have Object1 with a list of Object2...

    //navigation Property(s)
    public virtual ICollection<Object2> Object2s { get; set; }

Object2 has an Object1 property and an Object1_Id property

    public int Object1_Id { get; set; }
    public virtual Object1 Object1 { get; set; }

When I run the Index View for Object2 I get the message "Invalid column name 'Object1_Id1'". Weird....it's looking for an automated column. When I remove the Object1_Id from Object2, it does fine until I go to Save in the Create View. It then throws an error message:

{"Cannot insert the value NULL into column 'Object1_Id', table 'dbo.Object2'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

I'm sure I'm just missing an easy step. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-12-15 20:50:06

您希望使用流畅的 API 来进行调整。您可以在 DB Context 类中执行此操作。查看此链接页面的底部,查看使用 Fluent API 的示例。

http://www.asp.net/entity-framework/tutorials/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

    public class ObjectContext : DbContext
    {
        public DbSet<Object1> Object1s{ get; set; }
        public DbSet<Object2> Object2s{ get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Object2>()
                .HasOptional(o => o.Object1).WithRequired(o => o.Object2);
        }
    }

You want to use fluent API to make the adjustment. You can do this in your DB Context class. Look toward the bottom of the page of this link to see a sample of using fluent API.

http://www.asp.net/entity-framework/tutorials/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

    public class ObjectContext : DbContext
    {
        public DbSet<Object1> Object1s{ get; set; }
        public DbSet<Object2> Object2s{ get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Object2>()
                .HasOptional(o => o.Object1).WithRequired(o => o.Object2);
        }
    }
年少掌心 2024-12-15 20:50:06

我想通了我的问题。当我在 Object2 控制器上调用 Create post 操作时,我检索父 Object1,为新 Object2 创建 Guid,将新 Object2 添加到 Object1.Object2s 集合,将 Object1 状态设置为已修改,然后保存数据库:

            var object1ToUpdate = db.Object1s.Where(
                    aObject1 => aObject1.Id.Equals(myId)
                ).Include(aObject1 => aObject1.Object2s).Single();
            string id = Guid.NewGuid().ToString();
            Object2.Id = Guid.NewGuid();
            object1ToUpdate.Object2s.Add(Object2);
            db.Entry(object1ToUpdate).State = EntityState.Modified;
            db.SaveChanges();

I figured out my question. When I call the Create post action on my Object2 Controller, I retrieve the parent Object1, create a Guid for my new Object2, add the new Object2 to the Object1.Object2s collection, set Object1 state to modified, then save the db:

            var object1ToUpdate = db.Object1s.Where(
                    aObject1 => aObject1.Id.Equals(myId)
                ).Include(aObject1 => aObject1.Object2s).Single();
            string id = Guid.NewGuid().ToString();
            Object2.Id = Guid.NewGuid();
            object1ToUpdate.Object2s.Add(Object2);
            db.Entry(object1ToUpdate).State = EntityState.Modified;
            db.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文