当一个实体不在数据库上时,实体框架多对一

时间:2015-11-30 21:51:30

标签: entity-framework

我的模型上有两个相关的类。

java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.configureWebResources(AbstractGenericWebContextLoader.java:200)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:127)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:183)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:123)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: javax.servlet.SessionCookieConfig
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 32 more

该模型的表格如下

public class Project 
{
  public virtual int Id { get; set;}
  public virtual AdUser Creator { get; set; }
}

public class AdUser 
{
  public virtual string Id { get; set; }
  public List<string> Groups { get; set; }
}

CREATE TABLE [dbo].[Projects] ( [Id] INT IDENTITY (1, 1) NOT NULL, [CreatorId] VARCHAR(100) NOT NULL, CONSTRAINT [PK_Projects] PRIMARY KEY CLUSTERED ([Id] ASC) ); 类的数据源不是数据库,因为它代表Active Directory用户。因此,我想在上下文中调用AdUser后,只能在CreatorId列上存储Creator.Id属性。

另一方面,如果我从数据库中获取了一个项目,我只想要一个空SaveChanges()来设置Creator

主要思想是访问由独立存储库处理的Id数据。

我定位到Entity Framework 6.0

感谢

2 个答案:

答案 0 :(得分:1)

听起来好像要使用[NotMapped]属性

public class Project 
{
  public virtual int Id { get; set;}
  public virtual string CreatorId { get; set; }
  [NotMapped]
  public virtual AdUser Creator { get; set; }
}

这将告诉EF忽略模型上的Creator属性。

在我看来,你应该在这里为你的数据和应用程序逻辑使用不同的模型,例如,如果你想让一个不同的业务逻辑填充一个Creator,让它返回一个新的对象,不要把它放在datatier模型中。这个IMO更好地遵循SRP,即模型要么用于存储,要么不应用于两者。

答案 1 :(得分:0)

你可以将一个AdUserId映射到CreatorId私有(对于类消费者来说是一个很好的方法)并明确地映射项目。

public class Project 
{
  public virtual int Id { get; set;}

  private string AdUserId {
    get { // Read from AdUser 
        } 
    set { // set a backing field for 
          // AdUser lazy load or just set AdUser in an 
          // eagerly load approach 
        }

  // virtual is not required any more
  // If you need insert backing field for lazy load
  public AdUser Creator { get ; set; }

  public  ProjectMap : EntityTypeConfiguration<Project>
  {
    public  ProjectMap()
    {
        Ignore(t => t.AdUser);
        Property(t => t.AdUserId).HasColumnName("CreatorId");
    }

  }

}

public class AdUser 
{
  public virtual string Id { get; set; }
  public List<string> { get; set; }
}

在上下文课程中

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new Project.ProjectMap());
}