c# - 忽略属性不起作用 CassandraCSharpDriver

标签 c# cassandra cassandra-3.0

我使用实体模型中的一些属性来维护关系,我使用[Ignore]来忽略表中的该属性。

public class User : IdentityUser<Guid>
    {
        [Ignore]
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string CommonName { get; set; }
        public string ProfilePhoto { get; set; }
        public bool IsDeleted { get; set; }
        [Ignore]
        public virtual ICollection<UserRole> UserRoles { get; set; }
    }

var User = new Table<User>(dataSession);
                User.CreateIfNotExists();

当我尝试使用上面的代码创建时,出现错误。

enter image description here

问题:我是否使用了错误的脚本来创建表或错误的忽略方式?

提前致谢

最佳答案

检查您是否为 Ignore 属性使用了正确的命名空间。 Cassandra.Mapping.Attributes.Ignore 是正确的,另一个已弃用。

public class Program
    {
        public static void Main()
        {
            var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
            var session = cluster.Connect();

            var User = new Table<User>(session, MappingConfiguration.Global, "users", "testks");
            User.CreateIfNotExists();
            var u = new User
            {
                Id = Guid.NewGuid(),
                Password = "123",
                FirstName = "123",
                LastName = "123",
                CommonName = "123",
                ProfilePhoto = "321",
                IsDeleted = false,
                UserRoles = new List<UserRole>
                {
                    new UserRole
                    {
                        Text = "text"
                    }
                }
            };
            User.Insert(u).Execute();
            var result = User.First(l => l.Id == u.Id).Execute();
            Console.WriteLine(JsonConvert.SerializeObject(result));
            Console.ReadLine();
            User.Where(l => l.Id == u.Id).Delete().Execute();
        }
    }

    public class User : IdentityUser<Guid>
    {
        [Cassandra.Mapping.Attributes.Ignore]
        public string Password { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string CommonName { get; set; }
        public string ProfilePhoto { get; set; }
        public bool IsDeleted { get; set; }

        [Cassandra.Mapping.Attributes.Ignore]
        public virtual ICollection<UserRole> UserRoles { get; set; }
    }

    public class IdentityUser<T>
    {
        [Cassandra.Mapping.Attributes.PartitionKey]
        public T Id { get; set; }
    }

    public class UserRole
    {
        public string Text { get; set; }
    }

使用 C# 驱动程序 3.10.1 对 Cassandra 3.0.18 运行上面的代码似乎可以正常工作。 PasswordUserRoles 将不存在于表架构中,并且在执行 SELECT 语句时它们都将为 null Linq2Cql

关于c# - 忽略属性不起作用 CassandraCSharpDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57028043/

相关文章:

c# - 如何在 XAML 转换器中指定格式文化

java - 列删除会增加 Cassandra 中的读取延迟吗?

java - Cassandra Java Driver 将字段名称转换为小写

cassandra - Apache Cassandra 3.11.6 : clustering key error, cass-stress 写入后表中未定义的列名称

c# - Xamarin Resource.Designer.cs 不刷新新 ID 或事件

c# - 我可以自动初始化字典值吗?

cassandra - 带 Java 驱动程序的 DSE 图,如何添加边

node.js - 限制nodejs中cassandra db的并行请求数量

cassandra - 无法强制转换为格式化日期 - Cassandra 时间戳类型

c# - ASP.NET MVC : How does a controller distingush between parameters in the URL and sent by POST