相关疑难解决方法(0)

实体框架 - 代码优先 - 无法存储列表<String>

我写了这样的课:

class Test
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public List<String> Strings { get; set; }

    public Test()
    {
        Strings = new List<string>
        {
            "test",
            "test2",
            "test3",
            "test4"
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

和

internal class DataContext : DbContext
{
    public DbSet<Test> Tests { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

运行代码后:

var db = new DataContext();
db.Tests.Add(new Test());
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

我的数据正在保存但只是保存Id.我没有任何表格,也没有适用于字符串列表的关系.

我究竟做错了什么?我也尝试制作字符串, virtual但它没有改变任何东西.

谢谢您的帮助.

.net c# entity-framework

82
推荐指数
9
解决办法
7万
查看次数

如何在web.config文件中存储字典对象?

我想在我的Web配置文件中存储一个简单的键/值字符串字典.Visual Studio使得存储字符串集合变得容易(参见下面的示例),但我不确定如何使用字典集合来完成它.

        <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <string>value1</string>
          <string>value2</string>
          <string>value2</string>
        </ArrayOfString>
Run Code Online (Sandbox Code Playgroud)

.net asp.net dictionary web-config

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

如何在Settings.Default上保存List <string>?

我的表单上有一个ListBox,我想保存它并在我再次启动应用程序时加载值.

如何保存列表PrjName.Properties.Settings.Default?

.net c# winforms

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

如何在应用程序设置中存储对象列表

我最近熟悉了C#应用程序设置,看起来很酷.
我正在寻找一种存储自定义对象列表的方法,但我找不到方法!
实际上我看到了一个存储int []的帖子,但它对这个问题没有帮助.
我试图更改该解决方案的配置,以使其适合我的问题.它的XML配置文件是:

<Setting Name="SomeTestSetting" Type="System.Int32[]" Scope="User">
  <Value Profile="(Default)" />
</Setting>
Run Code Online (Sandbox Code Playgroud)

我尝试在type属性中引用我的对象,但它没有帮助,因为它没有识别我的对象...我尝试了"type = List"和"type ="tuple []"
这两个选项没帮我!

我有一个类看起来像:

class tuple
    {
        public tuple()
        {
            this.font = new Font ("Microsoft Sans Serif",8);
            this.backgroundcolor_color = Color.White;
            this.foregroundcolor_color = Color.Black;
        }
        public string log { get; set; }
        public Font font { get ; set; }
        public String fontName { get; set; }
        public string foregroundcolor { get; set; }
        public Color foregroundcolor_color { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# application-settings

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

StringDictionary不保存为用户设置

我创建了一个用户范围设置,类型为"System.Collections.Specialized.StringDictionary".每当我打开本地设置时,我都可以在配置中看到它,但它是空的.

我有其他正确保存的用户设置,但这个字典似乎根本没有保存.

为了获得字典保存,我需要做些什么吗?

.net c#

8
推荐指数
2
解决办法
5349
查看次数

有没有办法使用类似字典的集合作为应用程序设置对象?

我想在我的ASP.NET Web应用程序的应用程序设置中存储一组键/值对,但我找不到直接的方法来做到这一点.例如,这两个 问题告诉我,StringDictionary等不会序列化为XML,并建议我必须推出自己的实现.但似乎这应该更容易做到; 毕竟,web.config是XML,<applicationSettings>本质上是键/值对的集合,所以感觉我错过了一些明显的东西.鉴于我的具体情况如下,我是否真的必须推出自己的序列化,还是有更简单的解决方法?

有问题的Web应用程序是一个基本联系表单,它根据参数的值向不同的收件人发送电子邮件; 例如http://www.examplesite.com/Contact.aspx?recipient=support会发送电子邮件至SupportGroup@exampledomain.com.

目标是通过编辑web.config文件来添加或删除收件人(或更改其地址),这样我就不必重新编译,并且可以在测试和生产环境中轻松维护不同的配置.例如:

// I can use something like this for the sender address
SmtpMsg.From = New MailAddress(My.Settings.EmailSender)

// And then just edit this part of web.config to use 
// different addresses in different environments.
<setting name="EmailSender" serializeAs="String">
 <value>webcontact@exampledomain.com</value>
</setting>

// I want something like this for the recipients
SmtpMsg.To.Add(My.Settings.Recipients("support"))

// and presumably some sort of equivalent xml in web.config
// maybe something like this???
<recipients>
  <item name="support" serializeAs="String">
   <value>SupportGroup@exampledomain.com</value>
  </item>
  <!-- …
Run Code Online (Sandbox Code Playgroud)

vb.net asp.net dictionary

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

在应用程序设置中保存字典并在启动时加载它

Dictionary<string, double>当我重新启动应用程序时,我已经保存并恢复它.据我所知,你不能在c#中存储字典,所以试图解决方法.

目前我已经在设置中调用了字符串变量DB,并试图使用这样的东西:

var settings = dict.ToArray();

var prop = Properties.Settings.Default.DB;
Run Code Online (Sandbox Code Playgroud)

做一些像 prop = settings;

到目前为止,我已经做到了这一点,虽然它现在正在工作..

private void SaveSettings()
{
    var settings = spamPercentage.ToArray();

    var prop = Properties.Settings.Default.DB;

    string res = String.Join(",", settings);

    prop = res;
}

private void LoadSettings()
{
    var prop = Properties.Settings.Default.DB;

    var dictionary = prop.ToDictionary(item => item.Key,
                               item => item.Value);
}
Run Code Online (Sandbox Code Playgroud)

编辑字典中的值如下所示:

{[john, 0,53]}
{[ivone, 0,44]}
etc.
Run Code Online (Sandbox Code Playgroud)

@edit使用Julien JSON的想法,我做了:

 string prop = Properties.Settings.Default.DB;
    private void SaveSettings()
    {

        prop = JsonConvert.SerializeObject(dict);
Properties.Settings.Default.Save();
    }

    private …
Run Code Online (Sandbox Code Playgroud)

c# wpf settings dictionary converter

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