小编Nie*_*ein的帖子

DbContext.SaveChanges(); 不给出错误但不向数据库添加实体

我正在尝试使用以下代码将实体保存到我的数据库:

    public void Add(object entity)
        {
            DbContext.Entry(entity).State = System.Data.EntityState.Added;
            DbContext.SaveChanges();
        } 
Run Code Online (Sandbox Code Playgroud)

我测试过,如果出现问题(例如,实体的字段为空,在数据库中不能为空),则会出错.我填写了所有necesarry字段并调用了此代码.它没有给出错误.但是,该实体也未添加到数据库中.我怎样才能找到DbContext.SaveChanges(); 没有错误信息会出错?

下面是调用Add()函数的代码.

  public void StoreElectronicSignatureType(ElectronicSignatureTypeModel model)
    {
        var RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
        var parameters = RSA.ExportParameters(false);
        model.modulus = Convert.ToBase64String(parameters.Modulus);
        model.exponent = Convert.ToBase64String(parameters.Exponent);
        model.privatekey = RSA.ToXmlString(true);

        ElectronicSignatureType electronicSignatureType = new ElectronicSignatureType();
        Entity entity =  GetEntity(model.entity);
        electronicSignatureType.Entity = entity;
        electronicSignatureType.HashAlgorithm = model.hashAlgorithm;
        electronicSignatureType.Exponent = model.exponent;
        electronicSignatureType.Modulus = model.modulus;
        electronicSignatureType.Version = model.version;
        //electronicSignatureType.EntityId = entity.EntityId;
        electronicSignatureType.PrivateKey = StrToByteArray(model.privatekey);
        Add(electronicSignatureType);
    }
Run Code Online (Sandbox Code Playgroud)

c# linq savechanges dbcontext

3
推荐指数
1
解决办法
5324
查看次数

如何在R中舍入为1,1.5,2等而不是1,2或1.1,1.2,1.3?

我想将数字舍入到最接近的一半或整数.所以我想要围绕4.2到4,4.3到4.5和4.8到5.我尝试了一些圆形选项:

> round(4.34,1)
[1] 4.3
> round(4.34)
[1] 4
> round(4.34,0.5)
[1] 4.3
> round(4.34,2)
[1] 4.34
Run Code Online (Sandbox Code Playgroud)

所以我只知道如何增加有效数字的数量,而不知道如何进行不同类型的舍入.可以用圆函数完成,还是在R中有不同的函数?

r rounding

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

如何在不停止递归的情况下返回递归函数中的值?

我有一个列表中有x个列表的结构,每个列表都有x个元组.我事先不知道有多少嵌套列表,或者每个列表中有多少元组.

我想在所有元组中使用字典,因为我不知道列表的深度我想使用递归.我做的是

def tupleToDict(listOfList, dictList):
    itemDict = getItems(list)  # a function that makes a dictionary out of all the tuples in list
    dictList.append(itemDict)
    for nestedList in listOfList:
         getAllNestedItems(nestedList, dictList)

    return dictList
Run Code Online (Sandbox Code Playgroud)

这是有效的,但我最终得到了一个巨大的列表.我宁愿在每轮递归时返回itemDict.但是,我不知道如何(如果可能)返回值而不停止递归.

python recursion return

3
推荐指数
1
解决办法
2096
查看次数

Mac上的C中的分段错误,直接从教程中复制

我已经开始学习C和指针了,我一直在互联网上学习教程.我假设代码应该像教程一样工作,对我来说似乎是正确的,但是我得到了分段错误.代码是:

#include <stdio.h>
#include <stdlib.h>

/*
 * 
 */
int main(int argc, char** argv) {
    float fl = 3.14;
    unsigned int addr = (unsigned int) &fl;
    printf("fl's address=%u\n", addr);
    printf("addr's contents = %.2f\n", * (float*) addr);
    return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

/Applications/NetBeans/NetBeans       
6.9.1.app/Contents/Resources/NetBeans/ide/bin/nativeexecution/dorun.sh: line 33:  1626     
Segmentation fault      sh "${SHFILE}"
Run Code Online (Sandbox Code Playgroud)

这与我使用Mac有什么关系,或者代码有问题吗?

非常感谢,Niek

c macos segmentation-fault

2
推荐指数
1
解决办法
1083
查看次数

为什么我会得到"未捕获的ReferenceError:$未定义(匿名函数)"

我有一个cshtml页面,给出了这些错误:

Uncaught ReferenceError: $ is not defined               TabNotes:2
(anonymous function)                                    TabNotes:2
TabNotes:12Uncaught ReferenceError: ko is not defined   TabNotes:12
(anonymous function)                                    TabNotes:12
(anonymous function)                                    TabNotes:23
Run Code Online (Sandbox Code Playgroud)

什么会导致这样的错误?我找不到任何理由这样做.我尝试在$(document).ready(function(){中包装javascript函数,但这也没有用.代码如下

@model test.Web.Framework.Areas.Administration.Models.TabNotesModel 
@using (UI.DocumentReadyScript())
{    
    if (Model.meta.id.HasValue)
    {
        UI.jQuery("#tbl" + Model.meta.modelname).flexigrid(Model.Grid);
    }
}
<form method="post" action="@Url.Action("TabNotes", new { cmd = "refresh" })" id="@Model.meta.modelname">
<div class="ui-state-highlight ui-corner-all highlight" data-bind="visible: meta.message">
    <span class="ui-icon ui-icon-info"></span><strong data-bind="text: meta.message">
    </strong>
</div>
@using (UI.BeginBlock("Administation.TabNotes", UI.Label("Notes", "Notes").ToString(), test.Web.Framework.Core.enumIcons.pencil, false, false))
{
    <table id="@("tbl" + Model.meta.modelname)">
    </table>
} 
</form>
<script type="text/javascript">
(function() { …
Run Code Online (Sandbox Code Playgroud)

javascript razor knockout.js

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

if语句中无法识别xml.etree.cElementTree中的Type元素

我正在使用cElementTree来解析xml文件.使用.getroot()函数提供元素类型作为结果.我想在if语句中使用这种类型

if type(elementVariable) == 'Element':
     do stuff
Run Code Online (Sandbox Code Playgroud)

但是,当我执行以下操作时,无法识别该类型:

import xml.etree.cElementTree as xml
file = 'test.xml'
# parse the xml file into a tree
tree = xml.parse(file)
# Get the root node of the xml file
rootElement = tree.getroot()
return rootElement
print type(rootElement)
print type(rootElement) == 'Element'
print type(rootElement) == Element
Run Code Online (Sandbox Code Playgroud)

输出:

<type 'Element'>
False
Traceback (most recent call last):
  File "/homes/ndeklein/workspace/MS/src/main.py", line 39, in <module>
    print type(rootElement) == Element
NameError: name 'Element' is not defined
Run Code Online (Sandbox Code Playgroud)

所以

print type(rootElement) 
Run Code Online (Sandbox Code Playgroud)

给'元素'作为类型,但是

print …
Run Code Online (Sandbox Code Playgroud)

python types if-statement celementtree

2
推荐指数
1
解决办法
2372
查看次数

如何使用rpy2更改数据框的列名

我想做相当于这个R代码:

m2 <- cbind(1,2)
colnames(m2) <- c("x","Y")
Run Code Online (Sandbox Code Playgroud)

当我做

import rpy2.robjects as R
m2 = R.r['cbind'](1,2)
R.r['colnames'](m2) = R.StrVector(['x','y'])
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

SyntaxError: can't assign to function call
Run Code Online (Sandbox Code Playgroud)

我试过了

>>> m2 = R.r['colnames'](m2, R.StrVector(['x','y']))
>>> print m2

[1] "x1" "y2" 
Run Code Online (Sandbox Code Playgroud)

>>> params = {'do.NULL':False}
>>> m2 = R.r['colnames'](R.StrVector(['x','y']), m2, **params)
>>> print m2

[1] "11" "21"
Run Code Online (Sandbox Code Playgroud)

哪两个都没有给出我想要的结果.那么如何使用colnames来更改数据框的列名?

r dataframe rpy2

2
推荐指数
1
解决办法
796
查看次数

试图将对象解包到IEnumerable,获取IEnumerable是一个'类型'但是像'变量'错误一样使用

我想将一个对象取消装入IEnumerable.我检查是否可以为对象分配IEnumerable然后如果是,我想循环遍历对象中的值.但是,当我执行以下操作时:

if (typeof(IEnumerable<IRecord>).IsAssignableFrom(propertyValue.GetType()))
{
    foreach (var property in IEnumerable<IRecord>(propertyValue))
    {
        var test = property;
    }
}
Run Code Online (Sandbox Code Playgroud)

IEnumerable给出以下错误:

Error   1   'System.Collections.Generic.IEnumerable<test.Database.IRecord>' is a 'type' but is used like a 'variable'   D:\test.Test\ElectronicSignatureRepositoryTest.cs   397 46  test.Test
Run Code Online (Sandbox Code Playgroud)

如何将propertyValue指定为IEnumerable?

c# ienumerable casting

2
推荐指数
1
解决办法
601
查看次数

System.Configuration.ConfigurationManager.ConnectionStrings没有配置连接字符串

每当我尝试在我的C#代码中运行任何东西时,我都会收到以下错误:

System.InvalidOperationException was unhandled by user code
Message=No connection string configured
Run Code Online (Sandbox Code Playgroud)

它发生在以下代码中.

if (System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"] == null)
{
     throw new System.InvalidOperationException("No connection string configured");
}

connectionString = string.Format("{0};Application Name={1}", System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"].ConnectionString, this.applicationName);
Run Code Online (Sandbox Code Playgroud)

所以System.Configuration.ConfigurationManager.ConnectionStrings["DBContext"]是null.我真的找不到任何关于它的东西,一个可能相关的问题:如何修复"ConnectionString属性尚未初始化"表明配置文件可能有问题.此刻我恐怕不知何故意外删除了一个配置文件.

另外,请在文档中阅读:

Returns a ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration. 
Run Code Online (Sandbox Code Playgroud)

它包含默认值,所以我倾向于在配置文件中,我必须意外删除.

如果我是对的,我不知道它应该是哪一个,应该包含哪些内容.如果我错了,我不知道它来自哪里.那么System.Configuration.ConfigurationManager.ConnectionStrings ["DBContext"]在哪里设置?和/或我该如何解决这个问题?

c# configuration connection-string default visual-studio

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

如何在仍然限制 x 轴范围的同时更改 x 轴刻度?

我有以下示例数据框,我想从 -4、-1 绘制:

test_x <- c(-3.5, -2, -1, -0.5)
test_y <- c(1,2,3,4)
df <- data.frame(x=test_x, y=test_y)
library(ggplot2)
ggplot(df, aes(x=x, y=y)) + 
  geom_point() +
  xlim(-4, -1)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想显示 -4 刻度,我想排除 -0.5 点。但是,我也想更改 x 轴刻度标签。对于我发现的连续数据scale_x_continuous

ggplot(df, aes(x=x, y=y)) + 
  geom_point() +
  scale_x_continuous(breaks=c(-4, -3, -2, -1), labels=c("a","b","c","d"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是,这不显示a刻度,也不排除 -0.5 处的点。尝试使用 x_lim 再次限制它会产生错误 Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale

如何在仍然限制 x 轴范围的同时更改 x 轴刻度?

r ggplot2

2
推荐指数
1
解决办法
750
查看次数