我的用户表:
public class User
{
[Key]
public int UserId { get; set; }
public virtual ICollection<PollVote> PollVotes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的投票表:
public class Poll
{
[Key]
public int PollId { get; set; }
public virtual ICollection<PollVote> PollVotes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的投票表
public class PollVote
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int VoteId { get; set; }
[Key]
public int PollId { get; set; }
[Key]
public int UserId { get; set; }
public DateTime TimeVoted { get; set; } …Run Code Online (Sandbox Code Playgroud) 我面临着一个EF6 Code First上下文,其中有几个DbSetPOCO,它们之间有导航属性(和外键),例如:
public partial class Person
{
public Guid Id { get; set; }
public virtual ICollection<Address> Address { get; set; }
}
public partial class Address
{
public Guid Id { get; set; }
public Guid FK_PersonId { get; set; }
public virtual Person Person { get; set; }
}
modelBuilder.Entity<Person>()
.HasMany (e => e.Address)
.WithRequired (e => e.Person)
.HasForeignKey (e => e.FK_PersonId)
.WillCascadeOnDelete(false);
Run Code Online (Sandbox Code Playgroud)
鉴于这些类型,是否有任何适当的方式(即,不借助迭代通过反射和"猜测"的属性POCO /字段)以编程确定Address具有FK_PersonId指向Id的属性Person?
我首先使用实体框架6代码.
目前我有一个生产数据库.我想为测试添加一个不同的数据库.我怎么做?
我的测试项目中是否需要另一个连接字符串?
两个连接字符串之间应该有什么区别?
我尝试复制连接字符串并更改目录名称,但它导致映射到同一数据库.
请帮我理解连接字符串的每个部分
谢谢
我需要支架移植与add-migration从包管理器控制台与我的自定义迁移基地CustomMigration,其源自DbMigration。
public partial class NewMigration: CustomMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,我可以使用其他命令。我在Powershell脚本编写方面没有任何技能。我怎样才能做到这一点?
我正在关注这个问题:EF Code First - 1对1可选关系
我的关系设置如下:
public class Review {
[Key]
public int ReviewId { get; set; }
public virtual Payment Payment { get; set; }
}
public class Payment {
[Key]
public int PaymentId { get; set; }
[ForeignKey("Review")]
public int? ReviewId { get; set; }
public virtual Review Review { get; set; }
}
public class ReviewConfiguration : EntityTypeConfiguration<Review>
{
public ReviewConfiguration()
{
// One-to-One Optional
HasOptional<Payment>(s => s.Payment).WithOptionalDependent(s => s.Review).Map(s => s.MapKey("PaymentId"));
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个有效的密钥,但另一个从未映射为可选的FK:
我究竟做错了什么? …
我首先使用实体框架代码并添加了一个类,该类具有类的列表,其中类还必须具有另一个类列表,但是当我尝试通过迁移对数据库进行更新时,我得到:
SubscaleScore类型属性"SubscaleStatistics"上的ForeignKeyAttribute无效.在依赖类型"SubscaleScore"上找不到外键名称"SubscaleStatisticsId".Name值应该是以逗号分隔的外键属性名称列表.
这是我的课程的样子:
public class ExamStatistics : StatisticsData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int TestId { get; set; }
public IList<SubscaleStatistics> Subscales { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class SubscaleStatistics
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SubscaleStatisticsId { get; set; }
public int TestId { get; set; }
public int SubscaleNumber { get; set; }
public int ExamStatisticsId { get; set; }
[ForeignKey("ExamStatisticsId")]
public virtual ExamStatistics ExamStatistics { get; set; }
public IList<SubscaleScore> SubscaleScores …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2015中的Code-First进行数据库迁移.在此过程中,我已完成迁移步骤,直到添加迁移.
添加迁移后,我添加了这行代码
Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of DbContext1, Migrations.Configuration))
Run Code Online (Sandbox Code Playgroud)
在我的DbContext构造函数中设置数据库初始化程序,因为我以前错过了这一步.在那之后,我执行了
"Add-Migration initial -Force"
Run Code Online (Sandbox Code Playgroud)
在程序包管理器控制台中,因为我担心在添加迁移过程中需要此部分.然后,我直接执行
"Update-Database"
Run Code Online (Sandbox Code Playgroud)
问题是在我这样做之后,出现了一个错误
此操作需要连接到"主"数据库.无法创建与"主"数据库的连接,因为已打开原始数据库连接并且已从连接字符串中删除凭据.提供未打开的连接.
添加
重新启动计算机后,当我执行"更新数据库"时,上述错误不再出现.相反,出现了另一个错误:
数据库中已经有一个名为"something"的对象.
我看到了一个建议执行的答案线程
Add-Migration Initial -IgnoreChanges
Run Code Online (Sandbox Code Playgroud)
其次是
Update-Database -verbose
Run Code Online (Sandbox Code Playgroud)
我试过了两个但它仍然显示相同的错误.
vb.net ef-code-first sql-server-2012 asp.net-web-api visual-studio-2015
我需要过滤数据实体,但没有预定义的列,我将不得不过滤.
public class EventDocument
{
public string ID1 { get; set; }
public int ID2 { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Number { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我创建了所有需要的属性:SearchFirstName,SearchLastName,SearchNumber
现在我试图通过用户输入的这些参数来过滤EventDocument.如果用户在SearchFirstName中输入值,我的查询将如下所示:
var query = from b in DBContext.EventDocuments
where b.FirstName.Contains(SearchFirstName)
select b;
Run Code Online (Sandbox Code Playgroud)
如果用户输入SearchFirstName和SearchLastName的值,我的查询将如下所示:
var query = from b in DBContext.EventDocuments
where b.FirstName.Contains(SearchFirstName)
&& b.LastName.Contains(SearchLastName)
select b;
Run Code Online (Sandbox Code Playgroud)
如果我不知道如何构建查询 - 哪个归档用户将填写?也许他会为SearchLastName和SearchNumber输入值......
我想使用实体框架创建具有WHERE条件的UNIQUE INDEX。
public class Person {
public string Name { get; set; }
public bool Expired { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
条件是,仅当“ Expired”为假时,名称才是唯一的。
INDEX看起来像这样:
CREATE UNIQUE INDEX [IX_Person]
ON Person(Name)
WHERE [Expired] = 0;
Run Code Online (Sandbox Code Playgroud)
是否有某种方法可以使用代码优先的方式编写此唯一索引,而不必在迁移中执行此SQL?
我可以这样写:
[Index("IX_Expired", IsUnique = true)]
public bool Expired { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是我找不到指定“ WHERE”部分的方法。
我正在开发一个C#控制台应用程序,它从Guild Wars 2 API下载数据并使用Entity Framework 6将其输入到我的数据库中.我正在尝试使用多线程,以便我可以加快输入大量的数据进入我的数据库.
问题是当代码DBContext.SaveChanges()在我的AddRecipes方法中运行到我的调用时,返回以下错误:
违反PRIMARY KEY约束'PK_dbo.Items'.无法在对象'dbo.Items'中插入重复键.重复键值为(0).
以下是与我的问题相关的代码部分:
class Program
{
private static ManualResetEvent resetEvent;
private static int nIncompleteThreads = 0;
//Call this function to add to the dbo.Items table
private static void AddItems(object response)
{
string strResponse = (string)response;
using (GWDBContext ctx = new GWDBContext())
{
IEnumerable<Items> itemResponse = JsonConvert.DeserializeObject<IEnumerable<Items>>(strResponse);
ctx.Items.AddRange(itemResponse);
ctx.SaveChanges();
}
if (Interlocked.Decrement(ref nIncompleteThreads) == 0)
{
resetEvent.Set();
}
}
//Call this function to add to the dbo.Recipes table
private static …Run Code Online (Sandbox Code Playgroud) c# multithreading entity-framework primary-key ef-code-first
ef-code-first ×10
c# ×5
.net ×3
entity-framework-migrations ×1
foreign-keys ×1
linq ×1
localdb ×1
poco ×1
primary-key ×1
vb.net ×1