小编Mas*_*aus的帖子

实体框架,更新相关对象的问题

我目前正在使用最新版本的Entity Framework进行项目,我遇到了一个我似乎无法解决的问题.

在更新现有对象时,我可以相当容易地更新对象属性,直到它涉及到另一个类的引用的属性.

在下面的例子中,我有一个名为Foo的类,它存储各种属性,其中2个是其他类的实例

public class Foo
{
     public int Id {get; set;}
     public string Name {get; set;}
     public SubFoo SubFoo {get; set}
     public AnotherSubFoo AnotherSubFoo {get; set}
}
Run Code Online (Sandbox Code Playgroud)

当我使用下面的Edit()方法时,我传入了我想要更新的对象,我可以设法Name正确更新,但是我没有设法找到一种方法来更改SubFoo的属性.例如,如果SubFoo类具有属性Name,并且已更改并且在我的数据库与之间不同newFoo,则不会更新.

public Foo Edit(Foo newFoo)
{
    var dbFoo = context.Foo
        .Include(x => x.SubFoo)
        .Include(x => x.AnotherSubFoo)
        .Single(c => c.Id == newFoo.Id);

    var entry = context.Entry<Foo>(dbFoo);
    entry.OriginalValues.SetValues(dbFoo);
    entry.CurrentValues.SetValues(newFoo);

    context.SaveChanges();

    return newFoo;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助或指针将不胜感激.

更新:根据Slauma的评论我修改了我的方法

public Foo Edit(Foo newFoo)
{
    var dbFoo = …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first

57
推荐指数
1
解决办法
6万
查看次数

在Java中检查字符串中的字母大小写(上/下)

我遇到的问题是我无法让我的密码验证程序检查一个字符串,以确保其中一个字符为大写,一个字符为小写,它将检查整个字符串中的一个其他并根据它正在检查的语句打印错误消息.

我查看了这个网站和互联网的答案,我无法找到一个.这是功课.

以下是我目前的代码.

import java.util.Scanner;

public class password
{
    public static void main(String[] args)
    {
        Scanner stdIn = new Scanner(System.in);
        String password;
        String cont = "y";
        char ch;
        boolean upper = false;
        boolean lower = false;

        System.out.println("Setting up your password is easy. To view requirements enter Help.");
        System.out.print("Enter password or help: ");
        password = stdIn.next();
        ch = password.charAt(0);

        while (cont.equalsIgnoreCase("y"))
        {
            while (password.isEmpty())
            {
                System.out.print("Enter password or help: ");
                password = stdIn.next();       
            }

            if (password.equalsIgnoreCase("help"))
            {
                 System.out.println("Password must meet these …
Run Code Online (Sandbox Code Playgroud)

java

47
推荐指数
3
解决办法
18万
查看次数

Swift`in`关键字含义?

我正在尝试从parse.com实现一些代码,并且in在void之后我注意到一个关键字.

我很难过这是什么?你看到第二行了Void in

PFUser.logInWithUsernameInBackground("myname", password:"mypass") {
  (user: PFUser?, error: NSError?) -> Void in
  if user != nil {
    // Do stuff after successful login.
  } else {
    // The login failed. Check error to see why.
  }
}
Run Code Online (Sandbox Code Playgroud)

文档没有记录这一点.我知道in关键字用在for循环中.

有谁确认?

keyword ios swift

44
推荐指数
4
解决办法
2万
查看次数

EF Code First给我错误当IDENTITY_INSERT设置为OFF时,无法在表'People'中为identity列插入显式值.

我正在尝试实体框架4的代码优先(EF CodeFirst 0.8),并且遇到一个问题,一个简单的模型,在Person和之间有一个1 - - > 0..1的关系Profile.以下是它们的定义方式:

public class Person
{
    public int PersonId { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DOB { get; set; }

    public virtual Profile Profile { get; set; }
}

public class Profile
{
    public int ProfileId { get; set; }
    public int PersonId { get; set; }
    public string DisplayName { get; set; }

    public virtual Person …
Run Code Online (Sandbox Code Playgroud)

entity-framework entity-framework-4

42
推荐指数
5
解决办法
6万
查看次数

打字稿中*.d.ts与*.ts有什么区别?

我开始玩TypeScript,我发现它非常棒.但我感到困惑的区别*.d.ts*.ts.他们之间有什么区别?任何人都可以用适当的例子来解释我吗?

typescript typescript1.4

42
推荐指数
2
解决办法
2万
查看次数

如何将JSON对象解析为TypeScript对象

我目前正在尝试将我收到的JSON对象转换为具有相同属性的TypeScript类,但我无法使其工作.我究竟做错了什么?

员工类

export class Employee{
    firstname: string;
    lastname: string;
    birthdate: Date;
    maxWorkHours: number;
    department: string;
    permissions: string;
    typeOfEmployee: string;
    note: string;
    lastUpdate: Date;
}
Run Code Online (Sandbox Code Playgroud)

员工字符串

{
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": <anynumber>,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
    //I will add note later
}
Run Code Online (Sandbox Code Playgroud)

我的尝试

let e: Employee = new Employee();

Object.assign(e, {
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": 3,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
});

console.log(e);
Run Code Online (Sandbox Code Playgroud)

链接到Typescript …

json typescript angular

36
推荐指数
4
解决办法
11万
查看次数

Microsoft Windows Python-3.6 PyCrypto安装错误

pip install pycrypto 使用python3.5.2可以正常工作,但是由于python3.6而失败并出现以下错误:

inttypes.h(26):错误C2061:语法错误:标识符'intmax_t'

windows visual-studio pycrypto python-3.6

34
推荐指数
5
解决办法
3万
查看次数

在JavaScript中声明数组的方式是什么?

我只是学习JavaScript,似乎有很多方法可以声明数组.

  1. var myArray = new Array()
  2. var myArray = new Array(3)
  3. var myArray = ["apples", "bananas", "oranges"]
  4. var myArray = [3]

它们的区别是什么,首选方式是什么?

根据这个网站,以下两行非常不同:

var badArray = new Array(10); // creates an empty Array that's sized for 10 elements
var goodArray= [10];          // creates an Array with 10 as the first element
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这两条线做了两件完全不同的事情.如果您想添加多个项目,那么badArray将被正确初始化,因为Javascript会足够聪明地知道您正在初始化数组而不是说明您想要添加多少元素.

作者试图说的是Array(10)创建一个精确有10个元素[10]的数组,并创建一个未定义大小的数组,第0个元素是10?或者这是什么意思?

javascript arrays

33
推荐指数
4
解决办法
9万
查看次数

从Angular 2测试中更新输入html字段

我想在Angular 2单元测试中更改输入字段的值.

<input type="text" class="form-control" [(ngModel)]="abc.value" />
Run Code Online (Sandbox Code Playgroud)

我不能只改变ngModel因为abc对象是私有的:

 private abc: Abc = new Abc();
Run Code Online (Sandbox Code Playgroud)

在Angular 2测试中,我可以模拟用户在输入字段ngModel中键入内容,以便使用用户在单元测试中输入的内容进行更新吗?

我可以毫无问题地抓住输入字段DebugElementnativeElement输入字段.(只是设置了value对房地产nativeElement的输入域的似乎并没有工作,因为它不更新ngModel什么我为值设置).

也许inputDebugEl.triggerEventHandler可以被调用,但是我不确定给它的参数是什么它将模拟用户输入特定的输入字符串.

testing unit-testing jasmine angular

31
推荐指数
3
解决办法
3万
查看次数

如何调用base.base.GetHashCode()等二级基类方法

class A
{
    public override int GetHashCode()
    {
        return 1;
    }
}
class B : A
{
    public override int GetHashCode()
    {
        return ((object)this).GetHashCode();
    }
}

new B().GetHashCode()
Run Code Online (Sandbox Code Playgroud)

这会溢出堆栈.我怎么能叫Object.GetHashCode()B.GetHashCode()

编辑:B现在继承自A.

c# inheritance

29
推荐指数
3
解决办法
2万
查看次数