CTP5 - Code First不在数据库中生成关联集合 [英] CTP5 - Code First not generating associated collections in the Database

查看:71
本文介绍了CTP5 - Code First不在数据库中生成关联集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

采取以下简单类和关联的DbContext

Take the following simple class and associated DbContext

public class Test
{
	public Test()
	{
		Keywords = new Collection<string>();
	}

	public int Id { get; set; }
	public string Name { get; set; }
	public ICollection<string> Keywords { get; set; }
}

public class TestDbContext : DbContext
{
	public TestDbContext()
	{
		Database.Delete();
		Database.CreateIfNotExists();
	}

	public DbSet<Test> Tests { get; set; }

	protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
	{		
		base.OnModelCreating(modelBuilder);
	}
}





The type 'System.Collections.Generic.ICollection<string>' must be a non-nullable value type in order to use it as parameter 'T' 
in the generic type or method 'System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration<TStructuralType>
.Property<T>(System.Linq.Expressions.Expression<System.Func<TStructuralType,T>>)'

Any ideas how to create the necessary table relationships between test class and the keywords property?

推荐答案

如果您在数据库术语中考虑这一点,则无法存储集合就像这样的字符串。



相反,你应该创建一个类KeyWord,并让Test类有一个KeyWords列表。

类似于:

If you think about this in database terms, it is not possible to store a collection of strings just like that.

Instead, you should create a class, KeyWord, and make the Test class have a list of KeyWords.
Something like:

 


public class Test
{
	public Test()
	{
		Keywords = new Collection<KeyWord>();
	}

	public int Id { get; set; }
	public string Name { get; set; }
	public ICollection<KeyWord> Keywords { get; set; }
}


public class KeyWord
{ <br/>
       public int Id { get; set; }
    public string Word { get; set; }
    // ..
}


这篇关于CTP5 - Code First不在数据库中生成关联集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆