约束一对多关系(Constraint for one-to-many relationship)
我们有一个两对一的关系表。 我们希望强制执行一个约束条件,即给定的父记录至少存在一个子记录。
这可能吗?
如果不是,你会改变架构更复杂一点来支持这种约束吗? 如果是的话,你会怎么做?
编辑:我正在使用SQL Server 2005
We have a two tables with a one-to-many relationship. We would like to enforce a constraint that at least one child record exist for a given parent record.
Is this possible?
If not, would you change the schema a bit more complex to support such a constraint? If so how would you do it?
Edit: I'm using SQL Server 2005
原文:https://stackoverflow.com/questions/752023
最满意答案
@Ash的评论引导我回答。 所以现在我有了这个T4模板
<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".js" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="EnvDTE" #> <#@ include file="..\T4\Automation.ttinclude"#><# var project = VisualStudioHelper.GetProject("MyApp.Core.Contracts"); var contracts = GetSubClasses("MyApp.Core.Contracts.Commands.Command", project) .Concat(GetSubClasses("MyApp.Core.Contracts.Queries.Query", project)); #>(function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; <# foreach(var contract in contracts) { #> <# foreach(var part in BuildNameSpace(contract)) { #><#= part #> <# } var properties = GetProperties(contract).Select(p => CamelCased(p.Name)).ToList(); var args = string.Join(", ", properties); #> window.<#= contract.FullName #> = function(<#= args #>) {<# foreach(var property in properties) {#> this.<#= property #> = <#= property #>;<# } #> }; window.<#= contract.FullName #>.type = "<#= contract.FullName #>"; <# } #> })(window.MyApp = window.MyApp || {}); <#+ private static IEnumerable<string> BuildNameSpace(CodeClass @class) { return BuildNameSpace(@class.Namespace.Name.Split('.'), "window", new List<string>()); } private static IEnumerable<string> BuildNameSpace(IEnumerable<string> @namespace, string parent, List<string> parts) { var part = @namespace.FirstOrDefault(); if (part == null) return parts; var current = string.Format("{0}.{1}", parent, part); parts.Add(string.Format("{0} = ({0} || {{}});", current)); return BuildNameSpace(@namespace.Skip(1), current, parts); } public IEnumerable<CodeClass> GetSubClasses(string baseClass, Project project) { return VisualStudioHelper .CodeModel .GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false) .Cast<CodeClass>() .Where(c => GetInheritance(c).Any(b => b.FullName == baseClass) && !c.IsAbstract) .ToList(); } public IEnumerable<CodeClass> GetInheritance(CodeClass @class) { return GetInheritance(@class, new List<CodeClass>()); } public IEnumerable<CodeClass> GetInheritance(CodeClass @class, List<CodeClass> collection) { foreach(CodeClass @base in @class.Bases) { collection.Add(@base); GetInheritance(@base, collection); } return collection; } public string CamelCased(string pascalCased) { return pascalCased.Substring(0, 1).ToLower() + pascalCased.Substring(1); } public IEnumerable<CodeProperty> GetProperties(CodeClass @class) { if (@class == null) return new List<CodeProperty>(); var baseProperties = GetProperties(@class.Bases.Cast<CodeClass>().FirstOrDefault()); return baseProperties.Concat(@class .Members .Cast<CodeElement>() .Where(ce => ce.Kind == vsCMElement.vsCMElementProperty) .Cast<CodeProperty>() .Where(p => p.Access == vsCMAccess.vsCMAccessPublic)); } #>
它输出一个看起来像这样的JS,代码完成工作来自VS2012和Resharper
(function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; window.MyApp = (window.MyApp || {}); window.MyApp.Core = (window.MyApp.Core || {}); window.MyApp.Core.Contracts = (window.MyApp.Core.Contracts || {}); window.MyApp.Core.Contracts.Commands = (window.MyApp.Core.Contracts.Commands || {}); window.MyApp.Core.Contracts.Commands.FooCommand = function(bar) { this.bar = bar; }; window.MyApp.Core.Contracts.Commands.FooCommand.type = "MyApp.Core.Contracts.Commands.FooCommand"; })(window.MyApp = window.MyApp || {});
更新代码完成无法使用旧解决方案,必须添加静态声明每个闭包单独使其正常工作
@Ash's comment lead me to the answer. So now I have this T4 template
<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".js" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="EnvDTE" #> <#@ include file="..\T4\Automation.ttinclude"#><# var project = VisualStudioHelper.GetProject("MyApp.Core.Contracts"); var contracts = GetSubClasses("MyApp.Core.Contracts.Commands.Command", project) .Concat(GetSubClasses("MyApp.Core.Contracts.Queries.Query", project)); #>(function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; <# foreach(var contract in contracts) { #> <# foreach(var part in BuildNameSpace(contract)) { #><#= part #> <# } var properties = GetProperties(contract).Select(p => CamelCased(p.Name)).ToList(); var args = string.Join(", ", properties); #> window.<#= contract.FullName #> = function(<#= args #>) {<# foreach(var property in properties) {#> this.<#= property #> = <#= property #>;<# } #> }; window.<#= contract.FullName #>.type = "<#= contract.FullName #>"; <# } #> })(window.MyApp = window.MyApp || {}); <#+ private static IEnumerable<string> BuildNameSpace(CodeClass @class) { return BuildNameSpace(@class.Namespace.Name.Split('.'), "window", new List<string>()); } private static IEnumerable<string> BuildNameSpace(IEnumerable<string> @namespace, string parent, List<string> parts) { var part = @namespace.FirstOrDefault(); if (part == null) return parts; var current = string.Format("{0}.{1}", parent, part); parts.Add(string.Format("{0} = ({0} || {{}});", current)); return BuildNameSpace(@namespace.Skip(1), current, parts); } public IEnumerable<CodeClass> GetSubClasses(string baseClass, Project project) { return VisualStudioHelper .CodeModel .GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false) .Cast<CodeClass>() .Where(c => GetInheritance(c).Any(b => b.FullName == baseClass) && !c.IsAbstract) .ToList(); } public IEnumerable<CodeClass> GetInheritance(CodeClass @class) { return GetInheritance(@class, new List<CodeClass>()); } public IEnumerable<CodeClass> GetInheritance(CodeClass @class, List<CodeClass> collection) { foreach(CodeClass @base in @class.Bases) { collection.Add(@base); GetInheritance(@base, collection); } return collection; } public string CamelCased(string pascalCased) { return pascalCased.Substring(0, 1).ToLower() + pascalCased.Substring(1); } public IEnumerable<CodeProperty> GetProperties(CodeClass @class) { if (@class == null) return new List<CodeProperty>(); var baseProperties = GetProperties(@class.Bases.Cast<CodeClass>().FirstOrDefault()); return baseProperties.Concat(@class .Members .Cast<CodeElement>() .Where(ce => ce.Kind == vsCMElement.vsCMElementProperty) .Cast<CodeProperty>() .Where(p => p.Access == vsCMAccess.vsCMAccessPublic)); } #>
It outputs a JS that looks like this, code completion works from VS2012 with Resharper
(function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; window.MyApp = (window.MyApp || {}); window.MyApp.Core = (window.MyApp.Core || {}); window.MyApp.Core.Contracts = (window.MyApp.Core.Contracts || {}); window.MyApp.Core.Contracts.Commands = (window.MyApp.Core.Contracts.Commands || {}); window.MyApp.Core.Contracts.Commands.FooCommand = function(bar) { this.bar = bar; }; window.MyApp.Core.Contracts.Commands.FooCommand.type = "MyApp.Core.Contracts.Commands.FooCommand"; })(window.MyApp = window.MyApp || {});
Update Code completion didnt work with old solution, had to add the staticly declare each closure seperate for it to work correctly
相关问答
更多-
@Ash的评论引导我回答。 所以现在我有了这个T4模板 <#@ template debug="true" hostSpecific="true" #> <#@ output extension=".js" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> < ...
-
只读属性_DTE.Edition? https://docs.microsoft.com/en-us/dotnet/api/envdte._dte.edition?view=visualstudiosdk-2017 How about the readonly property _DTE.Edition? https://docs.microsoft.com/en-us/dotnet/api/envdte._dte.edition?view=visualstudiosdk-2017
-
我最终使用这种扩展方法让我再次工作 public static string GetFullName(this CodeTypeRef codeType) { string fullName; if (codeType.TypeKind == vsCMTypeRef.vsCMTypeRefArray) { CodeTypeRef arrayType = codeType.Element ...
-
它返回私人和公共成员。 但是只有那些在该级别声明的人,任何继承的成员等不会被退回。 It returns both private and public members. However only ones that are declared at that level, any inherited members etc. will not be returned.
-
EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0"); Project project = dte2.Solution.Projects.Item(1); Configuration configuration = project.ConfigurationManager.ActiveConfiguration; configura ...
-
我需要检测是否附加了调试器(如Ollydbg) 要检查进程是否附加了调试器,可以使用: CheckRemoteDebuggerPresent Debugger.IsAttached 如何检查调试器是否已附加 CheckRemoteDebuggerPresent适用于任何正在运行的进程并检测本机调试器。 Debugger.IsAttached仅适用于当前进程并仅检测托管调试器。 例如, OllyDbg将不会被检测到 。 码: using System; using System.Diagnostics; us ...
-
CodeElement具有Members属性,它是CodeElement的集合。 CodeElement有一个Kind属性,你可以从中知道我们正在谈论什么样的成员。 然后,您可以将每个成员转换为适当的界面并环顾四周。 大多数子类都有一个Type属性,包含您要查找的信息。 我在宏编辑器中的模块中键入了这个: Public Sub DisplayStuff() Dim objTextSel As TextSelection Dim objCodeCls As CodeClass obj ...
-
你检查过你的Project对象的Properties集合了吗? 根据这篇文章 , ProjectItem有一个“URL”项,因此Project也可能有一个...... 我承认这只是一个猜测,因为我对Web应用程序一无所知,我现在只是自己挖掘EnvDte的东西。 但也许它有帮助...... Have you checked the Properties collection of the Project object you've got? As per this post a ProjectItem has ...
-
VCProject用于C ++项目,为了使用与C#/ VB项目类似的接口,您必须使用VSProject 。 有许多VSLangProj重载/扩展,您必须找到特定于您需要使用的版本的那个。 请参阅: https ://msdn.microsoft.com/en-us/library/1xt0ezx9.aspx,适用于从2到100的所有VSLangProj接口(我认为是版本2到版本10)。 VCProject is for C++ projects, in order to use a similar inte ...
-
您是否在项目中添加了对ENVDTE和ENVDTE80(90等)的引用? Have you added a reference to ENVDTE and ENVDTE80 (90, etc) to your project?