【问题标题】:Ignore Attribute does't work CassandraCSharpDriver忽略属性不起作用 CassandraCSharpDriver
【发布时间】:2019-11-23 11:10:27
【问题描述】:

我在实体模型中使用一些属性来维护关系,我使用[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();

当我尝试使用上面的代码创建时,我得到了错误。

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

提前致谢

【问题讨论】:

    标签: c# cassandra cassandra-3.0


    【解决方案1】:

    检查您是否为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 将不存在于表架构中,并且在使用 Linq2Cql 执行 SELECT 语句时它们都将是 null

    【讨论】:

    • 您的答案是正确的,但是当我使用身份包时,我遇到了问题。 “Microsoft.Aspnetcore.Identity”。
    • 你有什么问题?您是否需要为该包的类添加属性?如果是这种情况,那么您有两个选择:1 - 创建自己的类以与 C# 驱动程序一起使用,并在执行数据库操作后手动执行映射 2 - 使用 Mapper 而不是 Linq2Cql 来配置映射:docs.datastax.com/en/developer/csharp-driver/3.10/features/…
    【解决方案2】:

    查看 Cassandra 到 C# 的类型列表。

    https://docs.datastax.com/en/developer/csharp-driver/3.7/features/datatypes/

    可能使用 IEnumerable 或什至代替 ICollection,这取决于您要执行 IDictionary 的目标。

    【讨论】:

    • 我使用哪种类型的属性并不重要,我只想让 cassandra 忽略这一点。你的答案是什么?我必须将属性更改为某些特定类型才能忽略?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多