我正在将经典的SQL数据库迁移到EF core 2.2,但遇到了问题。 我对EF并不陌生,但是我不知道该怎么做...
这是问题所在,我有一个表Users
,一个表Groups
和一个表Members
。成员是组成组的用户。我简化了模型,只保留了键:
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public class Member
{
[Key]
public int UserId { get; set; }
[ForeignKey(nameof(UserId))]
public virtual User User { get; set; }
[Key]
public int GroupId { get; set; }
[ForeignKey(nameof(GroupId))]
public virtual Group Group { get; set; }
}
public class Group
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ICollection<Member> Members { get; set; }
}
在经典SQL中,我可以通过键联接表,但是如何像Users
那样从Groups
跳到User.Groups
呢?
答案 0 :(得分:0)
用户已经有一个组,为什么需要将该组包括到Member中?仅仅拥有一个用户还不够吗?如果您需要从数据库中读取成员,则可以阅读它们并使用“包含”方法添加您的案例中的用户或组。像这样的事情会做的:
var result = db.Members
.Include(m => m.Groups)
.Include(m => m.User)
答案 1 :(得分:0)
好的,我用另一种方法解决了这个问题:
2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: RANDOMLY CHOSEN FILTER: size
2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: RANDOMLY CHOSEN FILTER VALUE: 9999
2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: Found value ------> 3350
2019-05-15 11:28:02,766 INFO o.a.j.m.J.JSR223 PreProcessor: Replacing it by: $13350|9999
2019-05-15 11:28:02,766 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Matcher.replaceFirst() is applicable for argument types: (java.lang.String, java.lang.String) values: [\Qsize=(.+?)&|size=(.+?)\$\E, \Q3350|9999\E]
Possible solutions: replaceFirst(java.lang.String)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_211]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Matcher.replaceFirst() is applicable for argument types: (java.lang.String, java.lang.String) values: [\Qsize=(.+?)&|size=(.+?)\$\E, \Q3350|9999\E]
Possible solutions: replaceFirst(java.lang.String)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:71) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136) ~[groovy-all-2.4.16.jar:2.4.16]
at Script36.run(Script36.groovy:23) ~[?:?]
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.16.jar:2.4.16]
... 9 more
User.Groups = context.Groups.Where(g => g.Members.Any(m => m.UserId == x.Id)).Select(g => new GenericData
{
Id = g.Id,
Tag = g.Tag,
Label = g.Name,
Flag = g.Flag
}).ToList()
是数据库用户
它的工作原理是这样,但是如果有人可以像我一开始那样说我该怎么做,我很想知道:)