热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过业务逻辑初始化Linq到Sql对象-InitializeaLinqtoSqlobjectviabusinesslogic

IwouldliketoextendaclassgeneratedbyLinqToSqltoinitializewhenanewobject(row)iscr

I would like to extend a class generated by Linq To Sql to initialize when a new object (=row) is created.

我想扩展由Linq To Sql生成的类,以便在创建新对象(=行)时进行初始化。

I want to add rows to a child table when a parent row is created.

我想在创建父行时向子表添加行。

I was hoping to use the Oncreated (partial) method do something like this:

我希望使用Oncreated(部分)方法做这样的事情:

partial class Product {
    partial void OnCreated() {
        // Fill default rows for FK relations
        this.Columns.Add(new ProductColumn {
            Name = "Name", ColumnType = 1
        });
        this.Columns.Add(new ProductColumn {
            Name = "Producer", ColumnType = 2
        });
    }
}

The OnCreated is called every time from the constructor. So also if the object will be loaded from the database after the call to OnCreated. And if the object is loaded from the database, do not want to execute the code.

每次从构造函数调用OnCreated。因此,如果在调用OnCreated之后将从数据库加载对象。如果从数据库加载对象,则不想执行代码。

So where can I add logic to my model to initialize an object(-graph)?

那么我在哪里可以添加逻辑到我的模型来初始化一个对象(-graph)?

4 个解决方案

#1


I think you'd be better off in this instance to use a Factory to create your instances; So anywhere you current have code like:

我认为在这种情况下你最好使用Factory来创建你的实例;所以你目前的任何地方都有如下代码

Product p = new Product();

You would instead have:

你会改为:

Product p = ProductFactory.CreateProduct();

And your CreateProduct() method would look something like:

你的CreateProduct()方法看起来像这样:

public Product CreateProduct()
{
    var p = new Product();
    p.Columns.Add(new ProductColumn {
        Name = "Name", ColumnType = 1
    });
    p.Columns.Add(new ProductColumn {
        Name = "Producer", ColumnType = 2
    });
    return p;
}

#2


There's no built in method for it in the Linq to SQL generated classes. They call OnLoaded specifically when called from the DB, but they do not call anything when it's not. I suspect because there's no way to know for sure...

在Linq to SQL生成的类中没有内置的方法。它们在从DB调用时专门调用OnLoaded,但是当它不是时,它们不会调用任何东西。我怀疑是因为没有办法确切知道......

I'd recommend using the factory approach that Chris suggested.

我建议使用Chris建议的工厂方法。

#3


With LINQ-to-SQL, the entities are largely unaware of the parent data-context - only lazy-loaded members like EntitySet know about this detail. You can identify data-context aware entity-sets by looking at .IsDeferred (assuming it is deferred) - this will be true if a data-context is involved, and false otherwise - but there are further problems:

使用LINQ-to-SQL,实体基本上不知道父数据上下文 - 只有像EntitySet 这样的延迟加载成员才知道这个细节。您可以通过查看.IsDeferred(假设它是延迟的)来识别数据上下文感知实体集 - 如果涉及数据上下文,则为true,否则为false - 但还有其他问题:

  • LINQ-to-SQL attaches the data-context (for deferred execution) after the constructur (and OnCreated) has executed
  • 在构造函数(和OnCreated)执行后,LINQ-to-SQL附加数据上下文(用于延迟执行)

  • If you add items manually in OnCreated/.ctor(), it breaks when LINQ-to-SQL attempts to attach the data-context
  • 如果在OnCreated / .ctor()中手动添加项目,它会在LINQ-to-SQL尝试附加数据上下文时中断

  • LINQ-to-SQL uses the default constructor, meaning that generics/new() etc will also share this code
  • LINQ-to-SQL使用默认构造函数,这意味着泛型/ new()等也将共享此代码

So unfortunately; no, I don't think you can do this in OnCreated in any sensible way. You could:

不幸的是;不,我认为你不能以任何明智的方式在OnCreated中做到这一点。你可以:

  • have a second constructor for this use, that adds the extra items
  • 有这个用途的第二个构造函数,它添加了额外的项目

  • use a factory method, i.e. a static .Create() method that builds items correctly
  • 使用工厂方法,即正确构建项目的静态.Create()方法

(since LINQ-to-SQL won't use either of the above itself)

(因为LINQ-to-SQL不会使用上述任何一种)

#4


If you are using the designer -- and I presume you are because you are using partial classes -- you can add the association in the designer and the appropriate columns will get added to your object automatically. MSDN has a reference on how to create associations using the designer work surface.

如果您正在使用设计器 - 我认为您是因为使用了部分类 - 您可以在设计器中添加关联,并且相应的列将自动添加到您的对象中。 MSDN提供了有关如何使用设计器工作表创建关联的参考。


推荐阅读
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • Oracle10g备份导入的方法及注意事项
    本文介绍了使用Oracle10g进行备份导入的方法及相关注意事项,同时还介绍了2019年独角兽企业重金招聘Python工程师的标准。内容包括导出exp命令、删用户、创建数据库、授权等操作,以及导入imp命令的使用。详细介绍了导入时的参数设置,如full、ignore、buffer、commit、feedback等。转载来源于https://my.oschina.net/u/1767754/blog/377593。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 我们有(据我所知)星型模式SQL数据库中的数据文件。该数据库有5个不同的文件,扩展名为 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
author-avatar
静默的温柔结成思念
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有