Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneToMany 一对多,怎么添加数据? #46

Closed
2881099 opened this issue Apr 28, 2019 · 14 comments
Closed

OneToMany 一对多,怎么添加数据? #46

2881099 opened this issue Apr 28, 2019 · 14 comments

Comments

@2881099
Copy link
Collaborator

2881099 commented Apr 28, 2019

实体

class Tag {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Name { get; set; }

    public int? Parent_id { get; set; }
    public virtual Tag Parent { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}
@2881099
Copy link
Collaborator Author

2881099 commented Apr 28, 2019

var repo = orm.GetRepository<Tag>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //需要手工开启

    var tag = new Tag {
        Name = "testaddsublist",
        Tags = new[] {
            new Tag { Name = "sub1" },
            new Tag { Name = "sub2" },
            new Tag {
                Name = "sub3",
                Tags = new[] {
                    new Tag { Name = "sub3_01" }
                }
            }
        }
    };
repo.Insert(tag);

约定

  • 当没有指明主键时,命名为 id 的字段将成为主键;(不区分大小写)

  • 当主键是 Guid 类型时,插入时会自动创建(有序、不重复)的值,所以不需要自己赋值;(支持分布式)

@linkdream1
Copy link

var tag = new A {
var tag = new Tag {

请问大神 new A{} 是什么语法呀?

@2881099
Copy link
Collaborator Author

2881099 commented Jan 10, 2020

var tag = new A {
var tag = new Tag {

请问大神 new A{} 是什么语法呀?

笔误,已改正

级联保存机制:https://github.com/2881099/FreeSql/wiki/%e8%81%94%e7%ba%a7%e4%bf%9d%e5%ad%98

@Bleibtreu
Copy link

创建一对多的时候,是都要这样固定命名吗,是否支持单向级联?
image

@Bleibtreu
Copy link

 class School_grade
    {
        [Navigate("School_class_Id")]
        [Column(IsIdentity = true, IsPrimary = true)]
        public int Id { get; set; }
        public string Grade { get; set; }
        public int School_class_Id { get; set; }

        public School_class School_class { get; set; }
        public ICollection<School_class> aaaa { get; set; }
    }

    class School_class
    {
        [Column(IsIdentity = true, IsPrimary = true)]
        public int Id { get; set; }
        public int aaaaSchool_gradeId { get; set; }
        public string Class { get; set; }
        //public School_grade School_Grade { get; set; }
    }

aaaa那一块,调试的时候说缺少这个字段,而且还是拼接固定值,请问这是必要的吗?

@2881099
Copy link
Collaborator Author

2881099 commented Apr 1, 2020

级联保存分两种:

1、追加保存,即不处理已存在的数据

var repo = fsql.GetRepository<ProjectEnergyType>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //需要手工开启
repo.Insert(pe);

2、完整保存,会对比老数据计算出添加、修改、删除执行

var repo = fsql.GetRepository<ProjectEnergyType>();
repo.Insert(pe);
repo.SaveMany(item, "EnergyTypes"); //在现有的数据上保存,内部会和现有数据进行对比,计算出应该插入、更新、删除的子记录

@2881099
Copy link
Collaborator Author

2881099 commented Apr 1, 2020

创建一对多的时候,是都要这样固定命名吗,是否支持单向级联?
image

命名没关系,这样的属于约定方式。如果不符合约定的命名,可以用 Navigate 特性绑定一个属性。

先说这个问题,什么是单向级联

@Bleibtreu
Copy link

刚刚没理解,现在好很多了,以 班级-年级 举例,单向存储的时候,我看到了可以存年级的时候存储子表的班级,
但是如果我同时存储的时候是否可以子表同时存储父表年级的字段?

@Bleibtreu
Copy link

而且我尚未理解您例子中的实体,为何要加虚方法关键字?

@2881099
Copy link
Collaborator Author

2881099 commented Apr 1, 2020

只能一对多,多对多级联保存。

单向属性保存可能带来很多问题。

virutal 主要作为是延时加载的导航属性,跟级联保存没关系。

@Bleibtreu
Copy link

好的,非常感谢!

@Bleibtreu
Copy link

还有一个问题,使用UseMonitorCommand只是成功的时候有SQL语句查看,但是SQL出错后,错误日志却只在vs输出框输出,如何让它在命令行打印日志?

 MonitorCommandExecuting = cmd => Console.WriteLine(cmd.CommandText)
 MonitorCommandExecuted = (cmd, s) => Console.WriteLine(s)

@2881099
Copy link
Collaborator Author

2881099 commented Apr 1, 2020

是 vs 得使用问题吗,弹出的异常窗口有一个设置

@Bleibtreu
Copy link

我找到问题了所在了,谢谢。

@2881099 2881099 closed this as completed May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants